2015-11-05 131 views
2

我有这个代码,我需要删除在列表视图中检查的数据,但我的问题是,我只得到第一个复选框。当我检查第二个复选框时,它不检索数据。 getChildCount()也返回0为什么它是这样的?我不知道我将如何继续解决我的问题。谢谢。Android从列表视图中获取复选框的值

这里是我的代码:

btnDel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RetailerDatabaseHelper dbHelper = new RetailerDatabaseHelper(TemplateActivity.this); 
      final SQLiteDatabase db = dbHelper.getReadableDatabase(); 
      CheckBox cb = (CheckBox) findViewById(R.id.checkBox1); //For further investigation! 

      Cursor c10 = db.query("Retailer", new String[]{"_id", "name", "validity"}, null, null, null, null, null, null); 
      ListAdapter adapter = new SimpleCursorAdapter(TemplateActivity.this, R.layout.custom_dialog_box, c10, new String[]{"name", "validity"}, new int[]{R.id.checkBox1, R.id.number}, 0); 
      ListView myList = (ListView) findViewById(R.id.listview); 
      myList.setAdapter(adapter); 

      Log.d("Inside Button", "Test Child: " + myList.getChildCount() + " Adapter: " + adapter.getCount() + " getAdapter: " + myList.getAdapter().getCount() + " Child: " + myList.getChildAt(1)); 

      for(int x = 0; x < myList.getAdapter().getCount(); x++) 
      { 
       View nextChild = myList.getChildAt(x); 
       if(nextChild instanceof CheckBox) 
       { 
        CheckBox checkbox = (CheckBox)nextChild; 
        if(checkbox.isChecked()) 
        { 
         Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      } 
     } 
    }); 
+0

请花时间capitilise你的'我的 –

回答

0

我会说前言本:从来没有做Android的用户界面/主线程上的数据库工作。它会阻止用户界面并导致应用程序冻结。查看this resource for more info的“主题”部分。

要解决您当前的问题,您可能需要维护一个“已检查”项目列表。这可能涉及到维护已使用a listener on your check boxes填充的id列表,您可以在列表的适配器视图绑定代码中设置此列表。在检查更改后,您可以将ID添加到您的列表中,删除后可以告诉数据库哪些ID需要删除。对于此解决方案,您可能需要调查制作自己的适配器。简单的光标适配器不会让你走得太远;)

此外,您可能想要查看RecyclerView - 更新版本的ListView。

+0

oooh。对不起,我是新的android编程这就是为什么我不知道我的方式。 – kathleen55

0

@ Kathleen51
我在BaseAdapter中使用SparseBooleanArray。编辑你的代码这样...

class OutPatientServiceAdapter extends BaseAdapter { 
    private LayoutInflater inflater; 
    private ArrayList<OutPatientService> beans; 
    private viewHolder holder; 
    private Context c; 
    private SparseBooleanArray booleanArray; 

    public OutPatientServiceAdapter(Context c, 
      ArrayList<OutPatientService> beans) { 
     super(); 
     this.c = c; 
     this.beans = beans; 
     inflater = LayoutInflater.from(c); 
     booleanArray = new SparseBooleanArray(beans.size()); 
     for (int i = 0; i < beans.size(); i++) { 
      booleanArray.put(i, true); 
     } 
    } 

    @Override 
    public int getCount() { 
     return beans.size(); 
    } 

    @Override 
    public OutPatientService getItem(int position) { 
     return beans.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, 
      ViewGroup parent) { 
     holder = new viewHolder(); 
     final OutPatientService bean = (OutPatientService) getItem(position); 
     if (convertView == null) { 
      convertView = inflater.inflate(
        R.layout.outpatient_service_item, parent, false); 
      holder.itemText = (CheckBox) convertView 
        .findViewById(R.id.itemText); 
      holder.doctorText = (TextView) convertView 
        .findViewById(R.id.doctorText); 
      holder.priceText = (TextView) convertView 
        .findViewById(R.id.priceText); 
      convertView.setTag(holder); 
     } else { 
      holder = (viewHolder) convertView.getTag(); 
     } 
     boolean check = booleanArray.get(position); 
     holder.itemText.setChecked(check); 
     holder.itemText 
       .setOnCheckedChangeListener(new OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         booleanArray.put(position, isChecked); 
        } 
       }); 
     holder.itemText.setText(bean.recipe_name); 
     holder.doctorText.setText(bean.create_doctor); 
     holder.priceText.setText(bean.fee); 
     return convertView; 
    } 

    public class viewHolder { 
     public CheckBox itemText; 
     public TextView doctorText; 
     public TextView priceText; 
    } 

    public SparseBooleanArray getmSpCheckedState() { 
     return booleanArray; 
    } 

    public void selectAll() { 
     for (int i = 0; i < beans.size(); i++) { 
      if (!booleanArray.get(i)) { 
       booleanArray.put(i, true); 
      } 
     } 
     notifyDataSetChanged(); 
    } 

    public void clearAll() { 
     for (int i = 0; i < beans.size(); i++) { 
      if (booleanArray.get(i)) { 
       booleanArray.put(i, false); 
      } 
     } 
     notifyDataSetChanged(); 
    } 
} 
+0

你好,如果数据来自数据库呢? – kathleen55

+0

不要使用SimpleCursorAdapter.you可以创建包含id,name,validity的自定义对象。然后将db.query转换为ArrayList <自定义对象>。 – tuder

+0

http://stackoverflow.com/questions/6853531/read-from-a-database-in-android-to-an-arraylist – tuder

相关问题