从SQLite设置联系人列表

时间:2012-08-21 11:35:19

标签: android sqlite listview adapter

我正在使用自己的适配器在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;
}
有人能帮帮我吗?我不知道如何显示我的联系人列表,这很重要。感谢。

1 个答案:

答案 0 :(得分:0)

您的适配器的getView似乎有误。看起来它每次调用时都会遍历整个游标。

您的适配器应该扩展SimpleCursorAdapter而不是ArrayAdapter,它在从数据库填充ListView时使用。有关示例,请参阅this post

该博客有一系列关于实现CursorAdapters的内容。您可以找到列表here