0
public class AppleSupportList extends BaseAdapter {

    private LayoutInflater mInflater;
    private final String[] values;

    public AppleSupportList(Context context, String[] values) {
        mInflater = LayoutInflater.from(context);
        this.values = values;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return values.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 position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.customlist1, null);
            holder = new ViewHolder();
            holder.text1 = (TextView) convertView.findViewById(R.id.labelContact1);
            holder.img = (ImageView) convertView.findViewById(R.id.imageContact1);
            holder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text1.setText(values[position]);
        holder.check.setId(position);

        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (holder.check.isChecked()) {
                    holder.check.setChecked(false);
                    //store id 
                } else {
                    holder.check.setChecked(true);
                    //remove id 
                }
            }
        });

        holder.check.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                CheckBox cb = (CheckBox) v;
                if (cb.isChecked()) {
                    //store id
                } else {
                    //remove id
                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView text1;
        ImageView img;
        CheckBox check;
    }
}

我正在创建一个带有复选框的自定义列表视图。所有工作正常,除非我滚动列表时复选框自动选中/取消选中。我如何存储复选框的状态。我已经解决了所有相关问题,但对我没有任何帮助。请看一下我的代码并帮助我。

4

1 回答 1

2

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

借鉴罗曼人的解决方案@

https://groups.google.com/forum/?fromgroups#!topic/android-developers/No0LrgJ6q2M

然后

     cb.setChecked(mCheckStates.get(position, false)); 
     cb.setOnCheckedChangeListener(this);

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

      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

<?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>
于 2013-08-07T11:16:43.420 回答