java - 使用光标适配器选中复选框时,使用删除按钮删除 ListView 中的项目

标签 java android android-sqlite

我遇到了这个问题。我正在使用光标适配器。我的问题是,当选中复选框时,它将获取值,然后单击按钮时,数据将被删除。顺便说一下,我用 ListView 显示复选框。

public class ListViewAdapter extends CursorAdapter {

SparseBooleanArray sba = new SparseBooleanArray();
public ListViewAdapter (Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    // when the view will be created for first time,
    // we need to tell the adapters, how each item will look
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.custom_dialog_box, parent, false);
    ViewHolder holder = new ViewHolder();
    holder.textView = (TextView) retView.findViewById(R.id.number);
    holder.cb = (CheckBox) retView.findViewById(R.id.checkBox1);
    retView.setTag(holder);
    return retView;
}

private static class ViewHolder {
    TextView textView;
    CheckBox cb;
}

@Override
public void bindView(View view, final Context context, Cursor cursor) {
    // here we are setting our data
    // that means, take the data from the cursor and put it in views
    final int position = cursor.getPosition();
    final CheckBox CheckBoxPersonName = (CheckBox) view.findViewById(R.id.checkBox1);
    CheckBoxPersonName.setTag(cursor);
    CheckBoxPersonName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked) {
                sba.put(position, true);
            }
            else {
                sba.put(position, false);
            }
        }
    });
    CheckBoxPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

    TextView textViewPersonPIN = (TextView) view.findViewById(R.id.number);
    textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(2))));
}

}

这是我的 mainActivity,将在其中单击按钮。

public class TemplateActivity extends Activity {

CheckBox cb;
Button btnSort, btnDel;

private ListViewAdapter listAdapter;
private RetailerDatabaseHelper dbHelper;
private ListView listView;

private static final String TAG = TemplateActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_template);

    btnSort = (Button) findViewById(R.id.btnSort);
    btnDel = (Button) findViewById(R.id.btnDelete);

    dbHelper = new RetailerDatabaseHelper(this);
    listView = (ListView) findViewById(R.id.listViewData);

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            System.out.println("Control: " + listView.getCheckedItemPositions());

            Cursor c = (Cursor) parent.getAdapter().getItem(position);
            String name = c.getString(1);
            String number = c.getString(2);
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(TemplateActivity.this);
            alertDialog.setTitle("RETAILER");
            alertDialog.setMessage("Are you sure you want to send " + name + " load to " + number + " ?");
            alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //insertData(textVal, value);
                    dialog.dismiss();
                }
            });
            alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            alertDialog.show();
        }
    });

    btnDel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int count = listView.getAdapter().getCount();
            SparseBooleanArray array = listView.getCheckedItemPositions(); /* 11/07/15 */
            System.out.println("Count: "+ count);

            /* 11/07/15 */
            for(int x = 0; x < array.size(); x++)
            {
                if(array.get(x)) /* 11/07/15 */
                {
                    Log.d(TAG, "Checked: " + listView.getTag());
                    Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                }

                else {
                    Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
                }
                listAdapter.notifyDataSetChanged();
            }
        }
    });

它正在工作,但是当未单击 ListView 数据时,复选框值不会显示。

最佳答案

您在此处将 SparseBooleanArray 声明为 null:

SparseBooleanArray array = null;

并且没有对数组进行赋值..并且您在 for 循环中使用它,那么它肯定会给您空引用错误。

关于java - 使用光标适配器选中复选框时,使用删除按钮删除 ListView 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33578936/

相关文章:

android - 在android中获取节点的各个值

java - 获取 Google Maps API 证书的 MD5 指纹时出现问题

Android Sugar ORM 尝试保存记录,成功,但貌似卡在了some look

java - 基于多个属性对List进行排序 Collection.sort() 编译错误

Java Scanner异常处理

java - 如何部署/检索模块化 Maven 项目的模块版本信息?

java - 在 BeanPropertyRowMapper 中注册属性转换器

php - 无法为对象输入值。导致 JSON 对象响应

android - 检查sqlite数据库在android中是打开还是关闭