我正在編寫一個代碼來顯示多選列表(一個有複選框),當用戶檢查一個項目時我想從數據庫表中檢索用戶的名稱和id,將其顯示在烤麪包上。下面的代碼:Listview for multiple selection only showing checkboxes and no display displayed
活動代碼
public class Add_To_Circle extends Activity {
ListView lv;
ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_to_circle);
// Get listview
lv = (ListView)findViewById(R.id.list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
... //all the other code,and then in another class in postexecute there is adapter:
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
//pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
Add_To_Circle.this, productsList,
android.R.layout.simple_list_item_multiple_choice, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
lv.setAdapter(adapter);
}
});
}
}
代碼XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
輸出:一個空白列表視圖中只有複選框,作爲attched形象。我究竟做錯了什麼?我都遵循相同的步驟教程網絡上仍沒有運氣:((
你可以使用自定義的適配器,每個行都有一個textview和複選框 – micky 2014-10-27 13:03:42
有沒有辦法讓這段代碼運行?當我編寫R.layout.list_item代替android.R.layout.simple_list_item_multiple_choice時,列表視圖具有所有的值,但是沒有複選框。你能告訴可能的原因嗎? – user3347803 2014-10-27 13:15:45
thatsy我只是說,而不是使用簡單的適配器,你可以使用自定義適配器,有一個textview和複選框 – micky 2014-10-27 13:18:49