2

我创建了一个包含 CheckedTextView 的 ListView。我正在通过自定义 ArrayAdapter 提供此 ListView 中的元素。列表显示正常,但是当我选择列表项时复选框没有响应

代码:

MainActivity 类:

public class MainActivity extends ListActivity {

    ArrayList<String> m_list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new AsyncHandler(this).execute();
    }


    public class AsyncHandler extends AsyncTask {

        Context context;

        public AsyncHandler(Context c) {
            context = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT)
                    .show();
        }

        @Override
        protected Object doInBackground(Object... arg0) {
            getList();
            return null;
        }

        @Override
        protected void onPostExecute(Object result) {
            // super.onPostExecute(result);
            setListAdapter(new ElementAdapter(context, m_list));
        }

        private void getList() {
            m_list = new ArrayList<String>();
            m_list.add("Ele 1");
            m_list.add("Ele 2");
            m_list.add("Ele 3");
            m_list.add("Ele 4");
        }
    }

}

适配器类

public class ElementAdapter extends ArrayAdapter implements OnClickListener {

    ArrayList<String> items = new ArrayList<String>();
    Context context;
    CheckedTextView tvElement;

    public ElementAdapter(Context c, ArrayList<String> elements) {
        super(c, R.layout.row, elements);
        this.items = elements;
        this.context = c;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.row, parent, false);
        tvElement = (CheckedTextView) view
                .findViewById(R.id.ctvElements);
        tvElement.setText(items.get(position));
        tvElement.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
        case R.id.ctvElements:
            if(!tvElement.isChecked()){
                tvElement.setChecked(true);
                tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_on_background);
            }else{
                tvElement.setChecked(false);
                tvElement.setCheckMarkDrawable(android.R.drawable.checkbox_off_background);
            }
            notifyDataSetChanged();
        }
    }
}

行布局:

<?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"
    android:orientation="horizontal">

    <CheckedTextView android:id="@+id/ctvElements"
        android:paddingLeft="20dip" android:paddingRight="20dip"
        android:paddingTop="10dip" android:paddingBottom="10dip"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical" android:checkMark="@android:drawable/checkbox_off_background"
        android:focusable="false" />

    <!-- <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:focusable="false" /> <TextView 
        android:id="@+id/tvElement" android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="Element" android:textSize="25sp" /> -->

</LinearLayout>
4

1 回答 1

4

如果您只想为每一行添加复选标记,请使用ListView.setChoiceMode()。复选标记将响应行中任意位置的单击,而不添加任何 onClickListeners 或自定义适配器。

将此添加到您的 onCreate 中:

ListView listView = (ListView) findViewById(android.R.id.list);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

然后使用内置的 ArrayAdapter:

    @Override
    protected void onPostExecute(Object result) {
        // super.onPostExecute(result);
        setListAdapter(new ArrayAdapter<String>(context, R.layout.row, m_list));
    }

您可以将默认选中的行布局与android.R.layout.simple_list_item_checked.

否则,如果您想要一些自定义 ListView 行将 row.xml 更改为:

<CheckedTextView android:id="@android:id/text1"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:layout_width="fill_parent"
        android:checkMark="@android:drawable/checkbox_off_background"
        android:gravity="center_vertical" 
        android:paddingLeft="20dip" 
        android:paddingRight="20dip"
        android:paddingTop="10dip" 
        android:paddingBottom="10dip"
        />
于 2012-07-09T17:18:56.430 回答