我正在使用自己的适配器在ListView
上列出我在SQLite
上的联系人的图片和姓名。我ListActivity
调用了ArrayAdapter
,但它没有调用getView
而我不知道为什么,然后我认为setListAdapter
是空的。< / p>
我从here获取样本,这是我的代码:
public class ListarContactos3 extends ListActivity {
private SQLiteDatabase database;
private ListAdapter dataSource;
private static final String fields[] = {"Nombre_Amigo","Picture","_id"};
Bitmap photo;
Cursor cur;
Context context;
private LayoutInflater mInflater;
View view;
ViewGroup parent;
public void onCreate(Bundle icicle){
super.onCreate(icicle);
DatabaseHandler helper = new DatabaseHandler(this);
database = helper.getWritableDatabase();
cur = helper.getAllContactList();
Log.d("ListarContactos3","Cursor :"+cur);
setListAdapter(new MyPerformanceArrayAdapter(this, cur));
Log.d("ListarContactos3","setListAdapter");
}
}
这是我的适配器:
public class MyPerformanceArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private Cursor cursor;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public MyPerformanceArrayAdapter(Activity context,Cursor cursor){
super(context, R.layout.row_def);
this.context = context;
this.cursor = cursor;
Log.d("MyPerformanceArrayAdapter","Constructor");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("MyPerformanceArrayAdapter","View");
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.row_def, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.Nombre_Amigo);
viewHolder.image = (ImageView) rowView
.findViewById(R.id.profile_picture);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
byte[] bb2= cursor.getBlob(4);
Bitmap bitmap = BitmapFactory.decodeByteArray(bb2, 0, bb2.length);
Log.d("MyPerformanceArrayAdapter","Bitmap: "+bitmap);
holder.image.setImageBitmap(bitmap);
holder.text.setText(cursor.getString(2));
rowView.setTag(holder);
}
return rowView;
}
}
这是我的DataBase方法:
public Cursor getAllContactList(){
String selectQuery = "SELECT * FROM " + TABLE_AMIGOS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
Contact contact = new Contact();
byte[] blob = cursor.getBlob(cursor.getColumnIndex(KEY_PIC_SQ));
Log.d("DatabaseHandler","blob: "+blob);
Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
Log.d("DatabaseHandler","blob: "+bmp);
contact.setPicSquare(bmp);
contact.setID(cursor.getString(0));
contact.setName(cursor.getString(2));
Log.d("DatabaseHandler","setname: "+cursor.getString(2));
}
db.close();
return cursor;
}
有人能帮帮我吗?我不知道如何显示我的联系人列表,这很重要。感谢。