Как добавить флажок в списке? - PullRequest
3 голосов
/ 29 марта 2010

У меня есть вопрос, я застрял на некоторое время, я не знаю, как я могу добавить флажок в список, например, если у меня есть список элементов, я хочу иметь возможность проверить их.мой код xml следующий:

<LinearLayout android:id="@+id/topLayout"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_centerHorizontal="true"  android:layout_height="wrap_content">

</LinearLayout>

<LinearLayout
    android:id="@+id/middleLayout"
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

    <LinearLayout android:id="@+id/leftMiddleLayout"
            android:orientation="vertical" android:layout_below="@+id/topLayout"
            android:layout_above="@+id/bottomLayout"
            android:layout_width="60px" android:layout_height="wrap_content"
            >

            <ListView android:id="@+id/checkboxList" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" ></ListView>

            <CheckBox android:id="@+id/checkbox"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:checked="false"
            android:text="test">
            </CheckBox>

    </LinearLayout>

    <LinearLayout android:id="@+id/rightMiddleLayout"
            android:orientation="vertical" android:layout_below="@+id/topLayout"
            android:layout_above="@+id/bottomLayout"
            android:layout_width="280px" android:layout_height="wrap_content"
            >

            <ListView android:id="@+id/list" android:layout_width="fill_parent"
                    android:layout_height="wrap_content" ></ListView>

            <TextView android:id="@+id/text" android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>                   
    </LinearLayout>
</LinearLayout>

<LinearLayout android:id="@+id/bottomLayout" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingBottom="5pt"
    >

    <EditText android:id="@+id/insertNewItem"
        android:layout_width="220px" android:layout_height="wrap_content" />

    <TextView android:layout_width="10px" android:layout_height="wrap_content" />

    <Button android:id="@+id/addItemButton" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:text="Add Item"/>
</LinearLayout>

если у вас есть идеи, пожалуйста, дайте мне знать, это для моих академических исследований: ((

Спасибо!

Ответы [ 5 ]

5 голосов
/ 20 июня 2012
final ListView lView = (ListView) findViewById(R.id.ListView01);
lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items));
lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

Здесь элемент представляет собой массив.

3 голосов
/ 30 марта 2010
public class myAdapter extends SimpleCursorAdapter {
    public myAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
        super(context, layout, cursor, from, to);  
}

@Override   
public void bindView(View view, Context context, Cursor cursor) {
    cb=(CheckBox)view.findViewById(R.id.cb);
    cb.setText(dbgetStringValue);
    cbText=(TextView)view.findViewById(R.id.cbText);
            cbText.setText(dbgetStringValue);
    cb.setChecked(false);
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton cb, boolean isChecked) {            
            if(cb.isChecked()) {                    
                // action
            }
            else if(isChecked==false) {
                // action
            }
        }           
    });
}
} 

это то, что вы хотите?

3 голосов
/ 29 марта 2010

Я рекомендую вам посмотреть это видео с сайта разработчика Android о том, как сделать ваш Android-интерфейс быстрым и эффективным . Он даст вам код для решения вашей проблемы и покажет, как правильно реализовать адаптер, чтобы он работал как можно быстрее.

0 голосов
/ 29 марта 2010

Или вы можете расширить любой ListAdapter до подкласса и переопределить bindView. Внутри, если вы установите .setText для ChecBoxes!

0 голосов
/ 29 марта 2010

Если вы используете ListAdapter (например, настроенный BaseAdapter) в вашем listView с помощью setAdapter (), вы можете использовать LayoutInflater внутри getView () вашего адаптера, чтобы предоставить любой макет, который вам нравится для записей в списке. Это может включать CheckBox.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...