我想用LinearLayout项目实现listview(它将包含CheckedTextView和多个textview)。
所以我想在ListView中使用LinearLayout而不是CheckedTextView。
我试过但单选按钮状态没有改变
我的代码:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemsCanFocus(false);
setListAdapter(new ArrayAdapter(this,R.layout.list_item,android.R.id.text1,COUNTRIES));
list_item
<CheckedTextView
.....
/>
我希望这样
list_item_new
<LinearLayout>
.....
<CheckedTextView/>
<TextView/>
.....
</LinearLayout>
答案 0 :(得分:0)
如果要自定义列表项的显示方式,则需要实现自己的适配器。这比你想象的要简单得多。以下是您的基本代码:
public class MyAdapter extends BaseAdapter {
List myData;
Context context;
public MyAdapter(Context ctx, List data) {
context = ctx;
myData = data;
}
public int getCount() { return myData.size(); }
public Object getItem(int pos) { return myData.itemAt(pos); }
public int getItemId(int pos) { return pos; }
public View getView(int position, View convertView, ViewGroup parent) {
//this is where you create your own view with whatever layout you want
LinearLayout item;
if(convertView == null) {
//create/inflate your linear layout here
item = ...;
}
else {
item = (LinearLayout) convertView;
}
//now create/set your individual components inside your layout based on your element
//at the requested position
...
}
}
这就是它的全部内容。