Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用游标适配器选中复选框时,使用“删除”按钮删除listview中的项_Java_Android_Android Sqlite - Fatal编程技术网

Java 使用游标适配器选中复选框时,使用“删除”按钮删除listview中的项

Java 使用游标适配器选中复选框时,使用“删除”按钮删除listview中的项,java,android,android-sqlite,Java,Android,Android Sqlite,我有点问题。我正在使用游标适配器。我的问题是,当选中复选框时,它将获得值,然后单击按钮时,数据将被删除。顺便说一下,我正在用listview显示复选框 public class ListViewAdapter extends CursorAdapter { SparseBooleanArray sba = new SparseBooleanArray(); public ListViewAdapter (Context context, Cursor c) { super(contex

我有点问题。我正在使用游标适配器。我的问题是,当选中复选框时,它将获得值,然后单击按钮时,数据将被删除。顺便说一下,我正在用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))));
}
}

这是我的主要活动,点击按钮

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();
            }
        }
    });
公共类TemplateActivity扩展活动{
复选框cb;
按钮btnSort,btnDel;
私有ListViewAdapter listAdapter;
私人零售数据库助手dbHelper;
私有列表视图列表视图;
私有静态最终字符串标记=TemplateActivity.class.getSimpleName();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_模板);
b传感器=(按钮)findViewById(R.id.b传感器);
btnDel=(按钮)findViewById(R.id.btnDelete);
dbHelper=新的RetailerDatabaseHelper(此);
listView=(listView)findViewById(R.id.listViewData);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
System.out.println(“控件:+listView.getCheckedItemPositions());
游标c=(游标)parent.getAdapter().getItem(位置);
String name=c.getString(1);
字符串编号=c.getString(2);
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(TemplateActivity.this);
alertDialog.setTitle(“零售商”);
setMessage(“您确定要将“+name+”加载发送到“+number+”?”);
alertDialog.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
//插入数据(textVal,值);
dialog.dismise();
}
});
alertDialog.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
alertDialog.show();
}
});
btnDel.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int count=listView.getAdapter().getCount();

SparseBooleanArray数组=listView.GetCheckEditePositions();/*11/07/15*/ System.out.println(“计数:+Count”); /* 11/07/15 */ 对于(int x=0;x

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

您在此处将SparseBooleanArray声明为空:

SparseBooleanArray array = null;

并且没有分配给数组..如果您在for循环中使用它,那么它肯定会给您空引用错误。

您在此处将SparseBooleanArray声明为空:

SparseBooleanArray array = null;

并且没有分配给数组。如果你在for循环中使用它,那么它肯定会给你空引用错误。

最好使用上下文操作栏,长时间单击,这是Android方式。嘿,检查一下。链接是GridView的,但你也可以对ListView做同样的操作。发布一个错误日志和至少一个屏幕截图来了解你的problem@VishalPatel下面是一个错误,最好使用一个长时间点击的上下文操作栏,这是Android方式。嘿,检查这个。这个链接是GridView的,但你也可以为ListView做同样的事情。发布一个错误日志和至少一个屏幕截图来了解你的错误problem@VishalPatel这是错误哦。我如何将阵列从适配器获取到主适配器活动sir?SparseBooleanArray=listView.getCheckedItemPositions();当您单击listView而不是复选框本身时,它会工作。ohh。如何将数组从适配器获取到我的主活动sir?SparseBooleanArray=listView.getCheckedItemPositions();当您单击listView而不是复选框本身时,它会工作。