Java 创建自定义listView时如何存储复选框的值

Java 创建自定义listView时如何存储复选框的值,java,android,Java,Android,我正在创建带有复选框的自定义列表视图。除滚动列表时复选框会自动选中/取消选中外,所有操作都正常。 如何存储复选框的状态。 我已经回答了所有相关的问题,但没有任何效果。 请查看我的代码并帮助我解决问题。您的自定义适配器必须实现CompoundButton.OnCheckedChangeListener。使用SparseBooleanArray 从Romain guy的解决方案中提取@ 然后 然后使用选中状态将文本设置为复选框 cb.setChecked(mCheckStates.get

我正在创建带有复选框的自定义列表视图。除滚动列表时复选框会自动选中/取消选中外,所有操作都正常。 如何存储复选框的状态。 我已经回答了所有相关的问题,但没有任何效果。
请查看我的代码并帮助我解决问题。

您的自定义适配器必须实现
CompoundButton.OnCheckedChangeListener
。使用
SparseBooleanArray

从Romain guy的解决方案中提取@

然后

然后使用选中状态将文本设置为复选框

     cb.setChecked(mCheckStates.get(position, false)); 
     cb.setOnCheckedChangeListener(this);
使用以下示例作为参考,并根据您的要求进行修改。我没有在示例中使用viewholder。使用代码中使用的视图保持架

范例

      public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}
public类MainActivity扩展活动实现
AdapterView.OnItemClickListener{
整数计数;
专用CheckBoxAdapter-mCheckBoxAdapter;
字符串[]类型=新字符串[]{
“动作”、“冒险”、“动画”、“儿童”、“喜剧”,
“纪录片”、“戏剧”,
“外国”、“历史”、“独立”、“浪漫”、“科幻”,
“电视”,“惊悚片”
};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终ListView ListView=(ListView)findViewById(R.id.lv);
setItemsCanFocus(false);
setTextFilterEnabled(true);
setOnItemClickListener(this);
mCheckBoxAdapter=新的CheckBoxAdapter(此,类型);
setAdapter(mCheckBoxAdapter);
按钮b=(按钮)findViewById(R.id.button1);
b、 setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
StringBuilder结果=新建StringBuilder();

对于(int i=0;我能告诉我如何将搜索功能添加到此自定义listview您需要一个EditText,您需要一个自定义筛选器来过滤listview数据。在stackoverflow上搜索有很多问题。如果您发现任何问题,发布一个新问题,我相信社区会帮助您。@测试并根据您的要求检查此修改门厅
      public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }
@Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
     mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

}
public class MainActivity extends Activity implements
AdapterView.OnItemClickListener {
    int count;
private CheckBoxAdapter mCheckBoxAdapter;

String[] GENRES = new String[] {
    "Action", "Adventure", "Animation", "Children", "Comedy",
"Documentary", "Drama",
    "Foreign", "History", "Independent", "Romance", "Sci-Fi",
"Television", "Thriller"
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ListView listView = (ListView) findViewById(R.id.lv);

    listView.setItemsCanFocus(false);
    listView.setTextFilterEnabled(true);
    listView.setOnItemClickListener(this);
    mCheckBoxAdapter = new CheckBoxAdapter(this, GENRES);
           listView.setAdapter(mCheckBoxAdapter);
    Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            StringBuilder result = new StringBuilder();
            for(int i=0;i<GENRES.length;i++)
            {
                if(mCheckBoxAdapter.mCheckStates.get(i)==true)
                {
                    result.append(GENRES[i]);
                    result.append("\n");
                }

            }
            Toast.makeText(MainActivity.this, result, 1000).show();
        }

    });




   }

public void onItemClick(AdapterView parent, View view, int
position, long id) {
    mCheckBoxAdapter.toggle(position);
}

class CheckBoxAdapter extends ArrayAdapter implements CompoundButton.OnCheckedChangeListener
{  private SparseBooleanArray mCheckStates;
   LayoutInflater mInflater;
    TextView tv1,tv;
    CheckBox cb;
    String[] gen;
    CheckBoxAdapter(MainActivity context, String[] genres)
    {
        super(context,0,genres);
        mCheckStates = new SparseBooleanArray(genres.length);
        mInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        gen= genres;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return gen.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi=convertView;
        if(convertView==null)
         vi = mInflater.inflate(R.layout.checkbox, null); 
         tv= (TextView) vi.findViewById(R.id.textView1);

         cb = (CheckBox) vi.findViewById(R.id.checkBox1);
         tv.setText("Name :"+ gen [position]);
         cb.setTag(position);
         cb.setChecked(mCheckStates.get(position, false));
        cb.setOnCheckedChangeListener(this);
        return vi;
    }
     public boolean isChecked(int position) {
            return mCheckStates.get(position, false);
        }

        public void setChecked(int position, boolean isChecked) {
            mCheckStates.put(position, isChecked);

        }

        public void toggle(int position) {
            setChecked(position, !isChecked(position));
        }
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

         mCheckStates.put((Integer) buttonView.getTag(), isChecked);    

    }

}

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="34dp"
        android:text="TextView" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginRight="22dp"
        android:layout_marginTop="23dp" />

</RelativeLayout>