如何在android Listview中使用LinearLayout而不是CheckedTextView
我想用LinearLayout项实现listview(它将包含CheckedTextView和多个textview)。如何在android Listview中使用LinearLayout而不是CheckedTextView,android,android-listview,Android,Android Listview,我想用LinearLayout项实现listview(它将包含CheckedTextView和多个textview)。 所以我想在ListView中使用LinearLayout而不是CheckedTextView。 我尝试了,但单选按钮状态没有改变。 我的代码: 列表项 <CheckedTextView ..... /> 我想要这样的 列出新项目 <LinearLayout> ..... <Ch
所以我想在ListView中使用LinearLayout而不是CheckedTextView。 我尝试了,但单选按钮状态没有改变。
我的代码: 列表项
<CheckedTextView
.....
/>
我想要这样的列出新项目
<LinearLayout>
.....
<CheckedTextView/>
<TextView/>
.....
</LinearLayout>
.....
.....
如果要自定义列表项的显示方式,则需要实现自己的适配器。这比你想象的要简单得多。以下是您的基本代码:
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
...
}
}
这就是它的全部。我看不出这是如何解决问题的。问题是线性布局中的CheckedTextView在按下时不会改变状态?@GlennBech您尝试过这个吗?我尝试过使用线性布局和嵌套的checkedTextView,该视图由游标适配器充气。这是行不通的,这就是为什么我最后出现在这一页上。为什么自定义适配器可以工作?问题的根源在于LinearLayout没有实现可检查接口?()正确的格伦·贝奇。我已经在基于Aleks答案的onItemClick上实现了使用。但我的问题是如何实现线性布局而不是checkedTextView?。可能吗?
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
...
}
}