0

我有自己的适配器,ListView可以在我的SQLite. 我有ListActivity那个调用 my ArrayAdapter,但它没有调用 thegetView并且我不知道为什么,然后setListAdapter我认为是空的。

我从:这里得到样本,这是我的代码:

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;
}

}

这是我的数据库方法:

   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;
}

有人能帮助我吗?我不知道如何显示我的联系人列表,这很重要。谢谢。

4

1 回答 1

0

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

Your adapter should extend SimpleCursorAdapter instead of ArrayAdapter, it is used when populating ListViews from a database. See this post for an example.

That blog has a series on implementing CursorAdapters. You can find the list here.

于 2012-08-21T12:04:19.470 回答