2013-03-18 59 views
0

在我的Android應用程序中,我從SQLite數據庫中讀取了一些數據,並嘗試將其顯示到列表視圖中。這裏是我的代碼:ListView與Cusror適配器不顯示任何內容

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

我沒有發現的結果,它不會顯示在列表視圖的任何項目。有人可以說這個代碼有什麼錯誤,以及如何解決它?

+0

你確定你的光標包含的東西?例如log c.getCount() – 2013-03-18 12:03:18

+0

可以打印c.getCount(),from.size,to.size? – Harshid 2013-03-18 12:05:13

+0

是的,我沒有檢查它。光標爲空 – 2013-03-18 12:14:59

回答

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

如果吐司將被顯示,您的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; 
} 

這是問題。

+0

是的,對不起,你的光標是空的 – 2013-03-18 12:14:42

+0

@ ling.s oks所以現在你修好了,你能接受答案嗎?謝謝 – Sajmon 2013-03-18 12:30:17

+0

我有1個疑問,什麼是int變量?我不明白,它沒有正確顯示結果。它只顯示海海。但我從來沒有在我的代碼中使用任何海里 – 2013-03-18 12:37:05