2014-10-27 56 views
0

我正在編寫一個代碼來顯示多選列表(一個有複選框),當用戶檢查一個項目時我想從數據庫表中檢索用戶的名稱和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形象。我究竟做錯了什麼?我都遵循相同的步驟教程網絡上仍沒有運氣:((

+0

你可以使用自定義的適配器,每個行都有一個textview和複選框 – micky 2014-10-27 13:03:42

+0

有沒有辦法讓這段代碼運行?當我編寫R.layout.list_item代替android.R.layout.simple_list_item_multiple_choice時,列表視圖具有所有的值,但是沒有複選框。你能告訴可能的原因嗎? – user3347803 2014-10-27 13:15:45

+0

thatsy我只是說,而不是使用簡單的適配器,你可以使用自定義適配器,有一個textview和複選框 – micky 2014-10-27 13:18:49

回答

0

可以使用具有一個TextView的和checkbox.Here是鏈接自定義適配器,

http://www.javacodegeeks.com/2013/09/android-listview-with-adapter-example.html

http://androidcocktail.blogspot.in/2012/04/adding-checkboxes-to-custom-listview-in.html

,如果你想用簡單的數組適配器,

參考此鏈接,

Selecting multiple items in ListView

感謝

+0

謝謝你這麼多:)我試圖,希望它能解決。 – user3347803 2014-10-27 13:43:29

+0

不用客氣!!如果有效,請接受我的回答。 – micky 2014-10-27 13:46:03

0

好,會盡量給出一個答案,但我不得不更改適配器的答案,所以這部分是未經測試。

讓我們去自下而上的,這是對列表中的每個單一元素的XML佈局:

view_adapter_item_checklist.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/item" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
    android:gravity="center_vertical" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

使用CheckedTextView消除了需要處理的複選框選中/取消處理自己。

現在我的代碼中的ListView被放置在一個LinearLayout中,因此是0dp的高度。重要的部分是選擇模式:

<ListView 
    android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:choiceMode="multipleChoice" 
    android:drawSelectorOnTop="false" /> 

適配器真的很簡單。它根據上面的checktext XML獲取一個字符串和充氣列表行的列表。唯一的任務是設置視圖的文本,實際上是:

SimpleChecklistAdapter:

public class SimpleChecklistAdapter extends ArrayAdapter<String> { 
    private final Context context; 
    private final List<String> values; 

    public SimpleChecklistAdapter(Context context, List<String> values) { 
     super(context, R.layout.view_adapter_item_checklist, values); 
     this.context = context; 
     this.values = values; 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View rowView = inflater.inflate(R.layout.view_adapter_item_checklist, 
       parent, false); 

     CheckedTextView text = (CheckedTextView)rowView.findViewById(R.id.item); 
     text.setText(values.get(position)); 


     return rowView; 
    } 
} 

所有有剩下的就是使用新的花式清單適配器,而不是SimpleAdapter在你的代碼

+0

我把ListView和CheckedTextView放在同一個佈局中嗎? – user3347803 2014-10-27 13:46:53

+0

不,如果那不是很清楚..不,只是更新您現有的listview與'android:choiceMode =「multipleChoice」' – cYrixmorten 2014-10-27 14:47:27