2015-11-07 67 views
1

我對此有問題。我正在使用遊標適配器。我的問題是什麼時候複選框被選中,它會得到的值,然後當一個按鈕被點擊時,數據將被刪除。順便說一句,我用listview顯示覆選框。當使用cursoradapter檢查複選框時,使用刪除按鈕刪除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(); 
      } 
     } 
    }); 

它正在工作,但是當列表視圖數據未被點擊時,複選框值不顯示。

+0

更好地使用長按的上下文操作欄,這是Android的方式。 –

+0

嘿檢查這[鏈接](http://androhub.com/select-and-share-multiple-images/)。該鏈接適用於GridView,但您也可以爲ListView做同樣的事情。 –

+0

發佈錯誤日誌和至少一個截圖,以瞭解您的問題 –

回答

0

您聲明SparseBooleanArray爲空在這裏:

SparseBooleanArray array = null; 

,並沒有分配到數組..和你在使用它的循環,那麼它一定會讓你空引用錯誤。

+0

ohh。我將如何從適配器獲取數組到我的mainactivity先生? – kathleen55

+0

SparseBooleanArray array = listView \t \t \t \t \t .getCheckedItemPositions(); – lacs

+0

當你點擊列表視圖而不是複選框本身時,它正在工作。 – kathleen55