使用cursoradapter选中复选框时,使用删除按钮删除列表视图中的项目

时间:2015-11-07 03:51:28

标签: 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();
            }
        }
    });

它正在工作但是当没有点击列表视图数据时,复选框值没有显示。

1 个答案:

答案 0 :(得分:0)

您在此处将SparseBooleanArray声明为null:

SparseBooleanArray array = null;

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