我需要一些自定义列表视图适配器的帮助。我正在使用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));
}
,但是当我运行该代码时,我的列表视图中没有得到任何结果。
所以有人可以建议我如何使用此适配器在我的活动中设置imageview和textview中的文本和图像。
非常感谢!
答案 0 :(得分:0)
检查出来
String text = cursorTitle.getString(cursorTitle.getColumnIndex("title"));
Log.i("title text","title text : "+text);
String[] names = new String[] { text };
LogCat中“文字”的结果是什么? 存储在'names'数组中的任何值?
答案 1 :(得分:0)
你在哪里使用适配器中的String数组,你传递给它?我想,你在两个文本视图中都设置了null。你需要像适配器中的holder.textview.setText(names [position])一样使用它。第二件事,你应该使用for循环从cursor创建你的字符串数组。看起来你总是得到你的游标数据的第一个结果。
答案 2 :(得分: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
,text
和text2
:
holder.textView.setText(text);
holder.textView2.setText(text2);
holder.imageView.setImageBitmap(image);
并在您的Activity中初始化它。就是这样。
希望它有所帮助!