Listview with Cursoradapter什么都没有显示

时间:2013-03-18 12:00:23

标签: java android simplecursoradapter

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

1 个答案:

答案 0 :(得分: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;
}

这是问题。