0

在我的 android 应用程序中,我从 SQLite 数据库中读取了一些数据并尝试将其显示到列表视图中。这是我的代码:

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 中显示任何项目。有人能说出这段代码中的错误是什么以及如何解决吗?

4

1 回答 1

0

代码看起来不错。很可能你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;
}

这是问题。

于 2013-03-18T12:04:57.347 回答