내 맞춤 목록보기 어댑터에 대한 약간의 도움이 필요합니다. 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에서 아무것도 얻지 못한다.
누구나 나를 제안 할 수 있습니다 어떻게 이미지 및 텍스트 뷰 내 텍스트 및 이미지를이 어댑터를 사용하여 내 활동에서 설정할 수 있습니다.
고맙습니다.
logcat? 커서가 비어 있습니까? sqlite 테이블에 데이터가 있습니까? 텍스트를 ""로 설정하고 있습니다. 그 이미지는 귀하의 sdcard에 존재하지 않을 수 있습니다 ... –
Logcat에서 어떤 오류도 발생하지 않으며 이미지는 sdcard에 있습니다. 문제가 없습니다. 테이블에 데이터가 있으며 커서가 비어 있지 않습니다. . –