2011-06-14 87 views
3

我知道這個問題已經被問及之前回答了,但我已經遵循多個教程無濟於事。在Android中使用SimpleCursorAdapter填充列表視圖

我只是想,如果我把我的代碼,也許有人可以幫助,並指出我在正確的方向。我不會說謊,我在這方面是一個大規模的noob,但是到目前爲止我都很喜歡它(當然,至少在這一點上)。所有這一切都發生的事情是,當我加載活動,它只是強制關閉並出現以下錯誤信息:

06-14 20:04:08.162: ERROR/AndroidRuntime(17605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.magic8/com.example.magic8.AnswerList}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView 

所以,在這兒呢。我的數據庫查詢是:

public Cursor fetchAnswersList() { 
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, "answer"}, 
    null, null, null, null, null, null);  
} 

然後我的列表視圖代碼:

public class AnswerList extends Activity { 

private Context context; 
private DatabaseManager mDbHelper; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.answerlist);   

    Cursor answers = mDbHelper.fetchAnswersList(); 
    startManagingCursor(answers); 

    ListView answerList=(ListView)findViewById(R.id.answerList);  

    String[] from = new String[] {"_id", "answer"}; 
    int[] to = new int[] {R.id.answerItem}; 
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, R.layout.list_item, answers, from, to); 
    answerList.setAdapter(cursorAdapter); 

    answerList.setOnItemClickListener(
     new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 
      // When clicked, show a toast with the TextView text 
      setToast("Answer: " + ((TextView) view).getText()); 
     } 
    }); 
} 

最後但並非最不重要的,這是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:id="@+id/addAnswerForm" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal"> 
     <EditText 
     android:id="@+id/addAnswer" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content"> 
      <requestFocus></requestFocus> 
     </EditText> 
     <Button 
     android:text="Add" 
     android:id="@+id/addAnswer_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
     </Button> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/answers" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 
     <ListView 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:id="@+id/answerList" 
     android:layout_width="match_parent"> 
     <TextView 
      android:id="@+id/answerItem" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"> 
     </TextView> 
    </ListView> 
    </LinearLayout> 

</LinearLayout> 
+0

你嘗試使用替代活動ListActivity? – 2011-06-14 19:18:34

+0

嗨,是的,我有。我是否認爲如果活動中不止有一個列表,我需要使用Activity而不是ListActivity? 目前只有一個列表,但在XML中有一個文本框和按鈕,因爲我希望能夠允許用戶將自己的答案添加到列表中。 – javaMonkey 2011-06-14 19:36:00

回答

2

你所得到的錯誤是最有可能的,因爲你試圖嵌入一個TextView到一個ListView ...

<ListView 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:id="@+id/answerList" 
    android:layout_width="match_parent"> 
    <TextView 
     android:id="@+id/answerItem" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 
    </TextView> 
</ListView> 

從ListView中刪除TextView的,然後嘗試以下...

String[] from = new String[] {"answer"}; 
int[] to = new int[] {android.R.id.text1}; 
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_list_item_1, answers, from, to); 

它使用預先定義的Android android.R.layout.simple_list_item_1android.R.id.text1

+0

謝謝你們兩位在發帖後這麼快回復我,我真的很感激。我現在有下面的代碼,我仍然得到一個強制關閉: 'ListView answerList =(ListView)findViewById(R.id.answerList); String [] from = new String [] {「answer」}; int [] to = new int [] {android.R.id.text1}; SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context,android.R.layout.simple_list_item_1,answers,from,to); answerList.setAdapter(cursorAdapter);' 我的錯誤信息: 您的內容必須有一個ListView,其id屬性爲'android.R.id.list' – javaMonkey 2011-06-14 20:04:08

+0

@javaMonkey:您是否將Activity更改爲ListActivity?如果是這樣,你只是稍微複雜一點。 – Squonk 2011-06-14 20:22:35

+0

很奇怪,現在我試圖得到這個錯誤: '06-14 21:23:17.852:ERROR/AndroidRuntime(19652):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.magic8/com .example.magic8.AnswerList}:java.lang.NullPointerException'。我已經研究過這意味着什麼,但我看不到任何導致它的事情。 – javaMonkey 2011-06-14 20:24:27

2

你往返需要被長度相同。您不需要在'from'數組中包含_id。請注意,您仍然需要在光標處添加_id。

+0

就像感謝你幫助我,我真的很感激! – javaMonkey 2011-06-14 20:11:02

相關問題