2015-03-19 19 views
0

已經做了很多搜索的使用下面的代碼我沒有其他帶出從SQLite數據庫表中的列表視圖後,提高列表視圖去除了得到由已知界解決。我提出simplecursoradapter的行導致它看起來的問題,我的應用程序停止。代碼中沒有其他錯誤。如果我評論simplecursoradapter這一行,這個錯誤不會出現,但是當然沒有listview。我無法使用simplecursoradaptor

Cursor c= vivzHelper.getAllRows();  
    String[] from = new String[]{ vivzHelper.NAME};  
    int[] to = new int[] {android.R.id.text1};  
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context,android.R.layout.simple_list_item_1,c,from,to,0);  
    ListView thislist=(ListView)findViewById(R.id.listView);  
    thislist.setAdapter(cursorAdapter);  


    public Cursor getAllRows() { 
    String where = null; 
    SQLiteDatabase db = helper.getWritableDatabase(); 
    String[] columns = { VivzHelper.UID,VivzHelper.NAME}; 
    Cursor c = db.query(VivzHelper.TABLE_NAME, columns, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
    } 
+2

應用程序停止。崩潰!檢查stacktrace並在此發佈 – Raghunandan 2015-03-19 05:16:39

+0

您是否對xml的單行使用custom_row.xml? – Keshav1234 2015-03-19 05:17:51

回答

0

進行以下更改,您將顯示一個列表視圖,並創建一個新的xml文件,其中包含您選擇的名稱。你只需要一個項目在列表視圖按照您的要求,因此,你可以給一個文本視圖,新的XML文件,如下圖所示:

//定義列表視圖的單行: custom_row.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/custom_listView" 
     android:orientation="horizontal"> 




     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Task1" 
      android:paddingLeft="20dp" 
      android:id="@+id/textView" /> 

</LinearLayout> 

在您的活動做到這一點,定義自定義行,感謝你們爲陣列指定自定義行:

Cursor c= vivzHelper.getAllRows();  
    String[] from = new String[]{ vivzHelper.NAME};  
    int[] to = new int[] {R.id.textView}; // textView of custom row. 
    SimpleCursorAdapter cursorAdapter; 
cursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.cutsom_row, c, from, to, 0);  
    ListView thislist=(ListView)findViewById(R.id.listView);  
    thislist.setAdapter(cursorAdapter);  


    public Cursor getAllRows() { 
    String where = null; 
    SQLiteDatabase db = helper.getReadableDatabase(); 
    String[] columns = { VivzHelper.UID,VivzHelper.NAME}; 
    Cursor c = db.query(VivzHelper.TABLE_NAME, columns, null, null, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    return c; 
    } 

這必須足以顯示你的列表視圖,讓我知道如果你仍然有問題。

+0

評論不適用於擴展討論;這個談話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/73356/discussion-on-answer-by-kesh1234-using-the-following-code-i-am-unable-to-提高-L)。 – Taryn 2015-03-19 14:17:23