【问题标题】:Android ListView With CheckBoxes带有复选框的 Android ListView
【发布时间】:2012-12-11 09:27:56
【问题描述】:

我找到了一个关于从 android 的数据库中填充 listView 的示例,它运行良好,但我想为这个应用程序添加一些功能,我想放一个 复选框 在我的 listview 中的每个项目旁边,当用户检查每个项目时,他将能够通过按确认按钮删除该项目。我已将这些行放在 >启用多选,但是复选框没有出现,不知道如何删除选中的Item!

ListView lstView = getListView();

            lstView.setChoiceMode(2);




    public void onListItemClick(
                    ListView parent, View v, int position, long id)
                    {
                    //---toggle the check displayed next to the item---
                    parent.setItemChecked(position, parent.isItemChecked(position));

                    }

你能帮我解决我的问题吗?

这是我的代码:

package com.saigmn;

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class DataListView extends ListActivity {

    private ArrayList<String> results = new ArrayList<String>();
    private String tableName = DBHelper.tableName;
    private SQLiteDatabase newDB;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        openAndQueryDatabase();

        displayResultList();


    }
    private void displayResultList() {
        TextView tView = new TextView(this);
        tView.setText("This data is retrieved from the database and only 4 " +
                "of the results are displayed");
        getListView().addHeaderView(tView);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);


        ////----------------------
        ListView lstView = getListView();
        //lstView.setChoiceMode(0); //CHOICE_MODE_NONE
        //lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE
        lstView.setChoiceMode(2);

       // setListAdapter(new ArrayAdapter<String>(this,
               // android.R.layout.activity_list_item));
    }

    //--------------------------------

    public void onListItemClick(
            ListView parent, View v, int position, long id)
            {
            //---toggle the check displayed next to the item---
            parent.setItemChecked(position, parent.isItemChecked(position));

            }


    private void openAndQueryDatabase() {
        try {
            DBHelper dbHelper = new DBHelper(this.getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("SELECT FirstName, Age FROM " +
                    tableName +
                    " where Age > 10 LIMIT 4", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("Name: " + firstName + ",Age: " + age);
                    }while (c.moveToNext());
                } 
            }           
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (newDB != null) 
                newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
        }

    }

}

这里是Link Of Example

【问题讨论】:

    标签: android database sqlite listview checkbox


    【解决方案1】:

    您应该使用您的自定义适配器,并且您可以通过其 getView 方法访问所有膨胀布局的控件。

    Listview with custom adapter containing CheckBoxes

    http://www.ezzylearning.com/tutorial.aspx?tid=1763429

    【讨论】:

      【解决方案2】:

      看看Android Api Demos。也许最好的方法是使用来自List11.java 的示例。如果您的行项目实现CheckableListView 将自行处理并保持选中的位置。并且不需要在您的Adapter 中处理它。

      【讨论】:

        【解决方案3】:

        ListView 创建CustomAdapter;创建layout_row,它代表ListView 中的行,并具有结构-[text] [radio_button]

        public class CustomAdapter extends BaseAdapter {
            private LayoutInflater inflater;
            private Activity activity;
            private ArrayList<String> listItems;
            private RadioButton listRadioButton = null;
        
            // NOTE: not the best practice to use static fields
            public static int selectedIndex;
        
            public CustomerListAdapter(Activity activity) {
                this.activity = activity;
                inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        
                listItems = new ArrayList<String>();
            }
        
            public void addItem(final String item) {
                listItems.add(item);
                notifyDataSetChanged();
            }
        
            @Override
            public View getView(final int position, View convertView, ViewGroup parent){
                ViewHolder holder;
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.list_row, null);
                    holder = new ViewHolder();
                    holder.text = (TextView)convertView.findViewById(R.id.text);
                    holder.radioButtonChooser = (RadioButton)convertView.findViewById(R.id.radioButtonChooser);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder)convertView.getTag();
                }
        
                holder.text.setText((listItems.get(position)).getText());
        
                holder.radioButtonChooser.setChecked(false);
        
                holder.radioButtonChooser.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                            // uncheck previous checked button. 
                            if (listRadioButton != null) 
                                listRadioButton.setChecked(false);
        
                            // assign to the variable the new one
                            listRadioButton = (RadioButton) v;
        
                            // find if the new one is checked or not, and save "selectedIndex"
                            if (listRadioButton.isChecked()) {
                               selectedIndex = position;
        
                                    // pass this index in your delete function
                                    // get item from your list by this index 
                                    // and delete row from list
                            } else {
                                            // nothing is selected
                                listRadioButton = null;
                                            selectedIndex = -1;
                            }
                    }
                });
        
                return convertView;
            }
        
            @Override
            public int getCount() {
                return listItems.size();
            }
        
            @Override
            public Object getItem(int position) {
                return listItems.get(position);
            }
        
            @Override
            public long getItemId(int position) {
                return position;
            }
        } 
        
            /**
             * Represents list row structure
             */
        public  class ViewHolder{
            public TextView text;
            public RadioButton radioButtonChooser;
        }
        

        ...

        private void fillList(){
                CustomAdapter adapter = new CustomAdapter(activity);
        
                ArrayList<String> items = new ArrayList<String>();
                        // fill "items" array with your list data
        
                for (String item : items) {
                    adapter.addItem(item);
                }
        
                listView.setAdapter(adapter);
        }
        

        ...

        list_row Layout 包含 2 个Views - TextView (id = text), RadioButton (id = radioButtonChooser)

        【讨论】:

          猜你喜欢
          • 2011-09-15
          • 1970-01-01
          • 1970-01-01
          • 2017-07-09
          • 2016-02-03
          • 1970-01-01
          • 2014-05-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多