SingleSelectList麻烦customView一行的onclicklistener麻烦、SingleSelectList、onclicklistener、customView

由网友(香烟如寂寞)分享简介:我想通过扩展的ListView与customrow像I am trying to construct SingleSelect list by extending ListView with customrow like 我想通过扩展的ListView与customrow像

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>

和$ C $下SingleSelectList像

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持有当前检查的项目,我想在点击行未选中该行,并检查newone。 有人可以给我知道我做错了什么?

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

推荐答案

您可以使用一个布尔值的ArrayList来维护您的复选框的状态。该ArrayList中的大小应该等于列表视图中的项目。您可以维护(选择/取消)将此ArrayList的onListItemClick()的状态,最重要的是,你应该使用此ArrayList中getView适配器()设置复选框的状态。在这里您可以使用作为ArrayList的索引位置。例如,

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));
 ...
}
阅读全文

相关推荐

最新文章