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 に答える