简体   繁体   English

如何在 android Listview 中使用 LinearLayout 而不是 CheckedTextView

[英]how to use LinearLayout instead of CheckedTextView in android Listview

i want to implement listview with LinearLayout item (it will contain CheckedTextView and multiple textview).我想用 LinearLayout 项目实现列表视图(它将包含 CheckedTextView 和多个文本视图)。
so i want use LinearLayout instead of CheckedTextView in ListView.所以我想在 ListView 中使用 LinearLayout 而不是 CheckedTextView。 i tried but the radio button state is not changing.我试过了,但单选按钮 state 没有改变。
my code:我的代码:

    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
        .....
        />

i want like this我想要这样
list_item_new list_item_new

<LinearLayout>
        .....
        <CheckedTextView/>
        <TextView/>
.....
</LinearLayout>

If you want to customise the way list items are appearing, you'll need to implement your own adapter.如果您想自定义列表项的显示方式,您需要实现自己的适配器。 It's much simpler than you think.它比你想象的要简单得多。 Here's the basic code for you:这是适合您的基本代码:

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
        ...
    }
}

and that's all it is to it.仅此而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM