SingleSelectList trouble with customView for one r

2019-01-29 10:56发布

问题:

I am trying to construct SingleSelect list by extending ListView with customrow like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:descendantFocusability="blocksDescendants"
    >
    <CheckBox
        android:id="@+id/chk"
        android:button="@drawable/q_list_check_box"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        />
    <TextView
        android:id="@+id/txtChoice"
        android:textColor="@color/white"
        android:layout_gravity="center_vertical|left"
        android:gravity="center_vertical|left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
</LinearLayout>

and with code for SingleSelectList like

public class SingleList extends ListView {

    int selected;
    List<String> data;

    public SingleList(Context context) {
        super(context);

        this.selected = -1;
        this.data = new ArrayList<String>();
    }

    public SingleList(Context context, AttributeSet attributes) {
        super(context, attributes);

        this.selected = -1;
        this.data = new ArrayList<String>();
    }

    public int getSelectedIndex() {
        return this.selected;
    }

    public void setSelectedIndex(int selected) {
        this.selected = selected;
    }

    public List<String> getData() {
        return this.data;
    }

    public void setData(List<String> data) {
        this.data = data;
        this.selected = -1;
    }

    public String getSelectedValue() {
        if (this.selected > -1)
            return this.data.get(selected);
        return "";
    }

    public void addDataItem(String item) {
        this.data.add(item);
    }

    public void initialize_list(List<String> data) {
        setData(data);
        this.setAdapter(new SingleListAdapter());
    }

    private class SingleListAdapter extends BaseAdapter {

        public SingleListAdapter() {
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return data.get(position).hashCode();
        }

        @Override
        public View getView(final int position, final View convertView,
                final ViewGroup parent) {

            View view = convertView;

            if (view == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.q_list_row_text, null);

            }
            final CheckBox chkChoice = (CheckBox) view.findViewById(R.id.chk);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    if (selected != -1) {
                        View tempView = SingleListAdapter.this.getView(
                                selected, convertView, parent);
                        CheckBox tempChk = (CheckBox) tempView
                                .findViewById(R.id.chk);
                        tempChk.performClick();
                    }

                    chkChoice.performClick();
                    selected = position;
                }
            });

            chkChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {

                    if (isChecked) {
                        ((View) chkChoice.getParent())
                                .setBackgroundResource(R.drawable.backgroun_checked);
                    } else {
                        ((View) chkChoice.getParent())
                                .setBackgroundResource(R.drawable.background_unchecked);
                    }

                }
            });

            TextView txtChoice = (TextView) view.findViewById(R.id.txtChoice);
            txtChoice.setText(data.get(position));

            return view;
        }
    }

}

// int selected holds current checked item and I want on click row to unchecked that row and checked newone. Can someone give me idea what I am doing wrong ?

回答1:

You can use a boolean ArrayList to maintain state of your check boxes. The size of that ArrayList should be equal to the list view items. You can maintain(select/deselect) the state of this ArrayList in onListItemClick() and most importantly, you should use this ArrayList in getView() of Adapter to set the state of checkbox. Where you can use "position" as index of ArrayList. e.g.

ArrayList<Boolean> lst_arrCheck =  new ArrayList<Boolean>();
@Override
public void onCreate(Bundle savedInstanceState)
{
   for (int i = 0; i < listItems.size(); i++) // Do this before setting the adapter e.g. in OnCreate()
   {
       lst_arrCheck.add(false);
   }
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked

    lst_arrCheck.set(position, !(lst_arrCheck.get(position)));
}
...
@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
  ...
  CheckBox cb_test = (CheckBox)findViewById(R.id.cb_test);
  cb_test.setChecked(lst_arrCheck.get(position));
 ...
}