Мне нужна небольшая помощь с моим настраиваемым адаптером представления списка. Я использую пример адаптера из vogella.de , но мне нужно найти способ, как установить текст и изображение из базы данных 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));
}
, но когда я запускаю этот код, я ничего не получаю в списке просмотра.
Так может ли кто-нибудь подсказать мне, как я могу установить текст и изображение в режиме просмотра изображений и текста в своей деятельности с помощью этого адаптера.
Большое спасибо!