簡體   English   中英

具有Cusror適配器的Listview不顯示任何內容

[英]Listview with Cusror adapter displays nothing

在我的Android應用中,我從SQLite數據庫中讀取了一些數據,並試圖將其顯示在listview中。 這是我的代碼:

ListView listContent;
SQLiteAdapterno nadapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ofrnumber);
listContent=(ListView)findViewById(R.id.listView1);
nadapter=new SQLiteAdapterno(this);
nadapter.openToRead();
Cursor c=nadapter.queueAll();
String[] from = new String[]{SQLiteAdapterno.KEY_ID, SQLiteAdapterno.KEY_RCODE, SQLiteAdapterno.KEY_RNAME,SQLiteAdapterno.KEY_OFNO};
int[] to = new int[]{R.id.id,R.id.text1,android.R.id.text2,android.R.id.text2};
SimpleCursorAdapter  cursorAdapter =new SimpleCursorAdapter(this, R.layout.row,c, from, to);
listContent.setAdapter(cursorAdapter);
}

SQLiteAdapterno:

public class SQLiteAdapterno {

public static final String MYDATABASE_NAME2 = "MY_DATABASEOFRN";
public static final String MYDATABASE_TABLE2 = "MY_OFFERNO";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_RCODE = "rcode";
public static final String KEY_OFNO = "ofno";
public static final String KEY_RNAME = "rname";
public static final String KEY_ID = "_id";

private static final String SCRIPT_CREATE_DATABASE1 =
          "create table " + MYDATABASE_TABLE2 + " ("
          + KEY_ID +" integer primary key autoincrement, "
          + KEY_RCODE + " text, "
          + KEY_RNAME + " text, "
          + KEY_OFNO + " text);";

private SQLiteHelper sqLiteHelper;
private  SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapterno(Context c)
{
    context=c;
}
public SQLiteAdapterno openToRead() throws android.database.SQLException {
      sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
      sqLiteDatabase = sqLiteHelper.getReadableDatabase();
      return this; 
     }
public SQLiteAdapterno openToWrite() throws android.database.SQLException {
      sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME2, null, MYDATABASE_VERSION);
      sqLiteDatabase = sqLiteHelper.getWritableDatabase();
      return this; 
     }
public void close(){
      sqLiteHelper.close();
     }
public long insert(String rcode, String rname, String ofno){
      ContentValues contentValues = new ContentValues();
      contentValues.put(KEY_RCODE, rcode);
      contentValues.put(KEY_RNAME, rname);
      contentValues.put(KEY_OFNO, ofno);
      return sqLiteDatabase.insert(MYDATABASE_TABLE2, null, contentValues);
     }
public  Cursor queueAll(){
      String[] columns = new String[]{KEY_ID, KEY_RCODE, KEY_RNAME, KEY_OFNO};
      Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE2, columns,
        null, null, null, null, null);
      return cursor;
     }
public int deleteAll(){
      return sqLiteDatabase.delete(MYDATABASE_TABLE2, null, null);
     }

我沒有找到結果,它在Listview中沒有顯示任何項目。 誰能說出這段代碼中的錯誤以及如何解決?

代碼似乎不錯。 您的Cursor很可能是空的。 嘗試添加簡單條件:

Cursor c = nadapter.queueAll();
if (c != null && c.getCount() > 0) {
   // set Adapter
}
else {
   Toast.makeText(this, "Cursor is empty", Toast.LENGTH_SHORT).show();
}

如果將顯示Toast,則您的getAll()方法不返回任何數據。

public  Cursor queueAll(){
   String[] columns = new String[] {KEY_ID, KEY_RCODE, KEY_RNAME, KEY_OFNO};
   Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE2, columns,
        null, null, null, null, null);
   return cursor;
}

這是問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM