2014-06-20 129 views
-1

我有一個問題,當我嘗試從我的listView中刪除多個選中的項目。如果我開始從下到上刪除項目從我的列表中刪除,但是當我從上到下或如果隨機項目被選中時出現問題。問題是檢查的項目不會被刪除,但未檢查的項目將被刪除。Android的 - 如何刪除多個項目從ListView與複選框?

public class MainActivity extends ActionBarActivity { 
private EditText etn,etl,etd; 
private Button add; 
private Button rmv; 
private ListView listView; 
private ArrayList<Data> list; 
private MyCustomAdapter dataAdapter = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu); 
    listView = (ListView) findViewById(R.id.listView1); 
    list = new ArrayList<Data>(); 
      add = (Button) findViewById(R.id.btn_add); 
      etn = (EditText) findViewById(R.id.edit_name); 
    eta = (EditText) findViewById(R.id.edit_lastname); 
    etd = (EditText) findViewById(R.id.edit_document); 
    rmv = (Button) findViewById(R.id.btn_delete); 

      displayView(); 



} 














public class Data { 
long document; 
String name; 
String lastname; 
boolean selected = false; 

public Data(long document, String name, String lastname, boolean selected){ 
    this.document=document; 
    this.name=nom; 
    this.lastname=lastname; 
    this.selected = selected; 
} 

public String getName(){ 
    return name; 
} 

public String getLastName(){ 
    return lastname; 
} 

public long getDocument(){ 
    return document; 
} 

public boolean isSelected() { 
     return selected; 
} 

public void setSelected(boolean selected) { 
     this.selected = selected; 
    } 
} 



















public void displayView(){ 
    String name = etn.getText().toString(); 
String lastname= eta.getText().toString(); 
long document = Integer.valueOf(etd.getText().toString()); 
Data edata = new Data(document,name,lastname,false); 
    list.add(edata); 
     dataAdapter = new MyCustomAdapter(this, 
     R.layout.list_info, list); 
     listView.setAdapter(dataAdapter); 

} 




public void delete(View view){ 
     deleteListItem(); 
} 



private void deleteListItem(){ 
if(list.isEmpty()){ 
    Toast.makeText(getApplicationContext(), 
      "No items to delete.", 
      Toast.LENGTH_LONG).show(); 
    return; 
} 
int itemCount = listView.getCount(); 
    for(int i=itemCount - 1 ; i>=0; i--){ 
    Data aux = list.get(i); 
    if(aux.isSelected()){ 
     dataAdapter.remove(aux); 
    } 
    } 
    dataAdapter.notifyDataSetChanged(); 
} 







private class MyCustomAdapter extends ArrayAdapter<Data> { 
      private ArrayList<Data> list; 

      public MyCustomAdapter(Context context, int textViewResourceId, 
      ArrayList<Data> list) { 
      super(context, textViewResourceId, list); 
      this.list = new ArrayList<Data>(); 
      this.list.addAll(list); 
      } 

      private class ViewHolder { 
      TextView name; 
      TextView lname; 
      TextView doc; 
      CheckBox ck; 
      } 

      @Override 
      public View getView(int position, View convertView, ViewGroupparent) { 

      ViewHolder holder = null; 
      if (convertView == null) { 
      LayoutInflater vi = (LayoutInflater)getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 
      convertView = vi.inflate(R.layout.list_info, null); 
      holder = new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.name); 
      holder.ck = (CheckBox) convertView.findViewById(R.id.checkBox1); 
      holder.lname = (TextViewconvertView.findViewById(R.id.lastname); 
      holder.doc = (TextView)convertView.findViewById(R.id.document); 

      convertView.setTag(holder); 
      holder.ck.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        CheckBox cb = (CheckBox) v ; 
        Data edata = (Data) cb.getTag(); 
        Toast.makeText(getApplicationContext(), 
        "Checkbox: " + cb.getText() + 
        " is " + cb.isChecked(), 
        Toast.LENGTH_LONG).show(); 
        edata.setSelected(cb.isChecked()); 
       } 
       }); 

      } 
      else { 
      holder = (ViewHolder) convertView.getTag(); 
      } 

      Data aux = list.get(position); 
      long document = aux.getDocument(); 
      holder.doc.setText("Doc:" + Long.toString(document)); 
      holder.name.setText ("       " + "Name:" + aux.getName()+ " "); 
      holder.lname.setText("Last name:" + aux.getLastName()); 
      holder.ck.setTag(aux); 
      holder.ck.setChecked(aux.isSelected()); 

      return convertView; 

      } 
     } 
+0

請點擊這裏:http://dj-android.blogspot.in/2013/02/multi-selection-listview-android-with.html –

+0

好的,我會看到的。謝謝你的回答。 – cuco

回答

1

我認爲問題在於該塊:

int itemCount = listView.getCount(); 
    for(int i=itemCount - 1 ; i>=0; i--){ 
    Data aux = list.get(i); 
    if(aux.isSelected()){ 
     dataAdapter.remove(aux); 
    } 
    } 

嘗試這樣的:

int itemCount = listView.getCount(); 
    for(int i=itemCount - 1 ; i>=0; i--){ 
    Data aux = dataAdapter.getItem(i); 
    if(aux.isSelected()){ 
     dataAdapter.remove(aux); 
    } 
    } 

注:我不知道,請嘗試,讓我知道結果如下: )

+0

非常感謝您閱讀和回答。我得到了修改該塊的相同結果。 – cuco

相關問題