2011-10-03 2 views
0

내 맞춤 목록보기 어댑터에 대한 약간의 도움이 필요합니다. vogella.de에서 어댑터 예제를 사용하고 있지만 sqlite 데이터베이스에서 텍스트 및 이미지를 설정하는 방법을 찾아야합니다. 여기에 내가 사용하고있는 어댑터 코드 :Android listview adapter sqlite의 텍스트 표시

import android.app.Activity; 
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MyArrayAdapter extends ArrayAdapter<String> { 
    private final Activity context; 
    private final String[] names; 
    Cursor cursor; 

    public MyArrayAdapter(Activity context, String[] names) { 
     super(context, R.layout.main_listview, names); 
     this.context = context; 
     this.names = names; 
    } 

    // static to save the reference to the outer class and to avoid access to 
    // any members of the containing class 
    static class ViewHolder { 
     public ImageView imageView; 
     public TextView textView,textView2; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // ViewHolder will buffer the assess to the individual fields of the row 
     // layout 

     ViewHolder holder; 
     // Recycle existing view if passed as parameter 
     // This will save memory and time on Android 
     // This only works if the base layout for all classes are the same 
     View rowView = convertView; 
     if (rowView == null) { 

      LayoutInflater inflater = context.getLayoutInflater(); 
      rowView = inflater.inflate(R.layout.main_listview, null, true); 

      holder = new ViewHolder(); 
      holder.textView = (TextView) rowView.findViewById(R.id.main_name); 
      holder.textView2 = (TextView) rowView.findViewById(R.id.main_info); 
      holder.imageView = (ImageView) rowView.findViewById(R.id.main_img); 
      rowView.setTag(holder); 

     } else { 
      holder = (ViewHolder) rowView.getTag(); 
     } 
     final Bitmap b = BitmapFactory.decodeFile("/sdcard/52dde26940e0d3081f6a086d4b54cd1c.jpg", null); 

     holder.textView.setText(""); 
     holder.textView2.setText(""); 
     holder.imageView.setImageBitmap(b); 


     return rowView; 
    } 

    public String[] getNames() { 
     return names; 
    } 
} 

와 나는이 같은 텍스트 뷰의 텍스트 설정하기 위해 노력하고있어 :

String sql = "SELECT title FROM collections"; 
     Cursor cursorTitle = userDbHelper.executeSQLQuery(sql); 
     if(cursorTitle.getCount()==0){ 
      Log.i("Cursor Null","CURSOR TITLE NULL"); 
     } else if(cursorTitle.getCount()>0){ 
      cursorTitle.moveToFirst(); 
      String text = cursorTitle.getString(cursorTitle.getColumnIndex("title")); 
      Log.i("title text","title text : "+text); 
      String[] names = new String[] { text }; 
      listView.setAdapter(new MyArrayAdapter(this, names)); 
} 

을하지만, 내가 그 코드를 실행하면 내가 돈 ' 결과적으로 내 listview에서 아무것도 얻지 못한다.

누구나 나를 제안 할 수 있습니다 어떻게 이미지 및 텍스트 뷰 내 텍스트 및 이미지를이 어댑터를 사용하여 내 활동에서 설정할 수 있습니다.

고맙습니다.

+0

logcat? 커서가 비어 있습니까? sqlite 테이블에 데이터가 있습니까? 텍스트를 ""로 설정하고 있습니다. 그 이미지는 귀하의 sdcard에 존재하지 않을 수 있습니다 ... –

+0

Logcat에서 어떤 오류도 발생하지 않으며 이미지는 sdcard에 있습니다. 문제가 없습니다. 테이블에 데이터가 있으며 커서가 비어 있지 않습니다. . –

답변

0

봅니다 같은 것을 할 수 있습니다 :

MyAddapterClass :

private final Activity context; 
    private final String[] names; 
    private final Bitmap image; 
    private final String text; 
    private final String text2; 
    Cursor cursor; 

    public MyArrayAdapter(Activity context, String[] names, Bitmap image, String text, String text2) { 
     super(context, R.layout.main_listview, names); 
     this.context = context; 
     this.names = names; 
     this.image = image; 
     this.text = text; 
     this.text2 = text2; 
    } 

및 설정하여 홀더에 image, texttext2 : 당신의 Activity.That 년대

holder.textView.setText(text); 
    holder.textView2.setText(text2); 
    holder.imageView.setImageBitmap(image); 

및 초기화 그것.

희망이 있습니다.

0

확인이 밖으로

String text = cursorTitle.getString(cursorTitle.getColumnIndex("title")); 
Log.i("title text","title text : "+text); 
String[] names = new String[] { text }; 

로그 캣에서 '텍스트'의 결과가 무엇입니까? 'names'배열에 저장된 값은 무엇입니까?

+0

결과 : "샘플 텍스트"- 내 sqlite 데이터베이스와 같습니다. 'cursor'와'text'는 null이 아닙니다. –

0

어댑터에서 String 배열을 어디에서 사용 했습니까? 둘 다 textview에서 null을 설정했다고 생각합니다. 어댑터의 holder.textview.setText (names [position])와 같이 사용해야합니다. 그리고 두 번째로, 커서에서 문자열 배열을 만드는 for 루프를 사용해야합니다. 커서 데이터의 첫 번째 결과 만 얻는 것처럼 보입니다.