2012-09-12 3 views
0

listview 형식의 이미지 목록을 보여주는 활동을 개발하고 싶습니다. BaseAdapter를 사용하여 코드 사용자 지정 어댑터가 있습니다. 에뮬레이터에서 실행하면 항상 디버거 창을 표시하고 에뮬레이터에는 아무 것도 표시되지 않습니다.Listview 용 사용자 지정 어댑터에서 오류가 발생합니다.

아래 지정된 폴더에 이미지 (11) 이미지가 있으며 사용자 권한 WRITE_EXTERNAL_STORAGE가 부여됩니다.

아래 코드는 다음과 같습니다. (ImageViewer.java)

package com.example.imageviewer; 

import java.io.File; 
import java.io.FileFilter; 
import java.sql.Array; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.zip.Inflater; 

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.media.audiofx.EnvironmentalReverb; 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Adapter; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 

public class ImageViewer extends Activity { 

    File oDirectory; 
    ListView oLVImages ; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_image_viewer); 

     try 
     { 
      oLVImages = (ListView) findViewById(R.id.lvImages); 

      String strPath = Environment.getExternalStorageDirectory().getPath() + File.separator + Environment.DIRECTORY_PICTURES; 

      oDirectory = new File(strPath); 
      Log.d("ImageViewer", strPath); 


      populateImages (oDirectory); 
     } 
     catch(Exception e){ 
      Log.d("ImageViewer", "1 " + e.getMessage()); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_image_viewer, menu); 
     return true; 
    } 


    public void populateImages (File oDirectory){ 
     try{ 
      File[] oFiles = oDirectory.listFiles(); 

      ArrayList<Image> oImages = new ArrayList<ImageViewer.Image>(); 

      for (int i = 0 ; i < oFiles.length;i++){ 
       if (oFiles[i].isFile()) 
       { 
        Image x = new Image(); 
        x.imgPath = oFiles[i].getPath().toString(); 
        x.imgName = oFiles[i].getName().toString(); 
        oImages.add(x); 
       } 
      } 

      ImageListAdapter oImageListAdapter = new ImageListAdapter(getApplicationContext(), oImages); 

      oLVImages.setAdapter(oImageListAdapter); 

     } 
     catch (Exception e){ 
      Log.d("ImageViewer", e.getMessage()); 
     } 
    } 

    class Image{ 
     private String imgName ; 
     private String imgPath; 

     public Image(){ 
      super(); 
      imgName = ""; 
      imgPath = ""; 
     } 

     public Image (String imgName, String imgPath){ 
      super(); 
      this.imgName = imgName; 
      this.imgPath = imgPath; 
     } 

    } 

    public class ImageListAdapter extends BaseAdapter{ 

     ArrayList<Image> imgImageList; 
     Context oContext; 

     public ImageListAdapter(Context oContext, ArrayList<Image> oImage){ 
      this.imgImageList = oImage; 
      this.oContext = oContext; 
     } 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return imgImageList.size(); 
     } 

     public Object getItem(int arg0) { 
      // TODO Auto-generated method stub 
      return this.imgImageList.get(arg0); 
     } 

     public long getItemId(int arg0) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     public View getView(int arg0, View arg1, ViewGroup arg2) { 
      // TODO Auto-generated method stub 
      //super.getView(arg0, arg1, arg2); 
      View oView = arg1; 
      LayoutInflater inflater = getLayoutInflater(); 
      oView = inflater.inflate(R.layout.imagedetail, null); 
      ImageView iv = (ImageView) findViewById(R.id.ivimage); 
      TextView tv = (TextView) findViewById(R.id.tvimage); 
      Image oIM = imgImageList.get(arg0); 
      Bitmap oBM = BitmapFactory.decodeFile(oIM.imgPath); 
      iv.setImageBitmap(oBM); 
      tv.setText(oIM.imgName.toString()); 

      return oView; 
     } 

    } 


} 

strings.xml의 파일 :

<resources> 

    <string name="app_name">ImageViewer</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_image_viewer">Image Viewer</string> 

    <string name="ivimage"></string> 
    <string name="tvheader">Photos</string> 

</resources> 

activity_image_viewer.xml 파일 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvheader" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFFFFFFF" 
     android:padding="10dp" 
     android:text="@string/tvheader" 
     android:textSize="22dp" 
     android:background="#FF336699" 
     /> 
    <ListView 
     android:id="@+id/lvImages" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    </ListView> 

</LinearLayout> 

imagedetail.xml (의 ListView의 각 항목에 대한 레이아웃 파일)

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1" 
     android:padding="10dp" 
     android:id="@+id/tbllayout" > 
     <TableRow 
      android:id="@+id/tblrow" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/ivimage" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:padding="10dp" 
      android:contentDescription="@string/ivimage" 
      /> 

     <TextView 
      android:id="@+id/tvimage" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      /> 
    </TableRow> 
</TableLayout> 
+0

코드 작업 중. 그 위대한. – rajeshwaran

+0

내 대답이 도움이 되셨습니까? – rajeshwaran

+0

작동하는 경우 대답을 수락하십시오. 감사합니다. – rajeshwaran

답변

0

이 코드를 다음에서 사용하십시오. 커스텀 어댑터

의 getView 방법

public View getView(int arg0, View arg1, ViewGroup arg2) { 
      // TODO Auto-generated method stub 
      //super.getView(arg0, arg1, arg2); 
      View oView = arg1; 
      LayoutInflater inflater = getLayoutInflater(); 
      oView = inflater.inflate(R.layout.imagedetail, null); 
      ImageView iv = (ImageView)oView.findViewById(R.id.ivimage); 
      TextView tv = (TextView)oView.findViewById(R.id.tvimage); 
      Image oIM = imgImageList.get(arg0); 
      Bitmap oBM = BitmapFactory.decodeFile(oIM.imgPath); 
      iv.setImageBitmap(oBM); 
      tv.setText(oIM.imgName.toString()); 

      return oView; 
     } 

레이아웃 사용 위젯을 팽창,

이미지 뷰의 IV = (이미지 뷰) oView.findViewById (R.id.ivimage);

TextView tv = (TextView) oView.findViewById (R.id.tvimage);

+0

BaseAdapter를 확장하고 여전히 오류가 발생하는 "ImageListAdapter"getView 메서드에서 동일한 코드를 사용했습니다. –

+0

오류 코드를 게시하십시오. – rajeshwaran

+0

"Perspective Switch 확인"을 표시하고 "예"를 클릭하면 디버그 모드가 열리고 "Thread [<1> main] (일시 중지됨 (예외 NullPointerException)) –

관련 문제