markdown ListView控件与ArrayAdapter

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown ListView控件与ArrayAdapter相关的知识,希望对你有一定的参考价值。

需要在xml中添加一个listview
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.deepinfar.www.myarxivreader.SelectActivity"
    android:orientation="vertical">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv">
    </ListView>
</LinearLayout>
```
对应此listview,添加其元素的list_subject.xml文件
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="8"
        android:layout_height="wrap_content"
        android:text="GUGU.dd"
        android:id="@+id/tv_subject"
        android:layout_marginLeft="5dp"/>
    <CheckBox
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/cb"/>
</LinearLayout>
```
添加覆写ArrayAdapter
```[java]
public class SubjectSelectAdapter extends ArrayAdapter<Subject> {
    Context mContext;
    List<Subject> subjectList = new ArrayList<>();
    TextView mTv;
    
    public SubjectSelectAdapter(@NonNull Context context, @NonNull List<Subject> objects) {
        super(context, 0, objects);
        mContext = context;
        subjectList = objects;
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Log.d("hehe","position:"+position);
        View listItem = convertView;
        if (listItem == null) {
            listItem = LayoutInflater.from(mContext).inflate(R.layout.list_selection, parent, false);
        }
        Log.d("hehe", subjectList.get(position).getName());
        mTv = (TextView) listItem.findViewById(R.id.tv_subject);
        mTv.setText(subjectList.get(position).getName());
        CheckBox mCb = (CheckBox) listItem.findViewById(R.id.cb);
        Log.d("adp",subjectList.get(position).toString());
        mCb.setChecked(subjectList.get(position).getIsSelected().equals("Y"));
        mCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox) v).isChecked()){
                    subjectList.get(position).setIsSelected("Y");
                    Log.d("hehe","set "+position+" to "+" Y:"+ subjectList.get(position).getIsSelected());
                }else{
                    subjectList.get(position).setIsSelected("N");
                    Log.d("hehe","set "+position+" to "+" N:"+ subjectList.get(position).getIsSelected());
                }
            }
        });

        return listItem;
    }
}

```

注意每次被隐藏时会触发checkbox的onChange事件,所以用onClick来替代onChange处理事件

最后,在Activity中ListView绑定

```
mLv.setAdapter(new SubjectSelectAdapter(this,subjectList));
```
如果数据会变化,不要用匿名对象,并使用
```
subjectAdapter.notifyDataSetChanged();
```
更新数据

以上是关于markdown ListView控件与ArrayAdapter的主要内容,如果未能解决你的问题,请参考以下文章

markdown 将ListView控件中的内容以Excel中导出

android开发--ListView

android开发--ListView

布局与控件-ListView知多少(上)

WPF自定义控件与样式-列表控件DataGrid与ListView自定义样式

布局与控件-ListView的Adapter们