2015-11-05 44 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

相關問題