2015-11-14 59 views
1

我在我的佈局中獲取listview ...我不知道如何從列表視圖中刪除多個項目,當我選中複選框中的項目並單擊刪除按鈕...任何人都可以幫忙嗎?我添加了一段代碼,請幫助。如何使用複選框刪除lisview項目?

public class classname extends Activity implements OnItemSelectedListener { 

ListView lv; 
ArrayList<x_obj> x_list = new ArrayList<x_obj>(); 
TextView delete; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_x); 
     del_txt = (TextView) findViewById(R.id.del_txt); 

del_txt.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      "code for delete selected items" 

     } 
    }); 
} 
public OrderAdapter(Context context, int row_layout, 
      ArrayList<x_obj> items) { 
     super(context, row_layout, items); 
     this.items = items; 
    } 

    @Override 
    public View getView(final int position, View convertview, ViewGroup parent) { 
     View v = convertview; 

     LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.row_layout, null); 
     final x obj = items.get(position); 

TextView txt_name = (TextView) v.findViewById(R.id.txt_name); 
    CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1); 

    return v; 

    } 
} 
+0

你是否在listitem旁邊顯示覆選框?在這種情況下,你可以循環你的數組列表並檢查一個項目是否被選中或不選中。如果選中,然後將其添加到不同的數組列表中,並用新的數組列表替換舊的數組列表並調用notifydatasetchanged。 –

+0

現在我收到了listview中的複選框..我沒有做代碼部分複選框..可以幫助我..我在這裏添加我的代碼 – learner

+0

我從web respons ...中獲取列表項並在x對象中添加項目...使用異步任務 – learner

回答

0
public class classname extends Activity implements OnItemSelectedListener { 

    ListView lv; 
    ArrayList<x_obj> new_al=new ArrayList<x_obj>(); 
    ArrayList<x_obj> x_list = new ArrayList<x_obj>(); 
     TextView delete; 
    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_x); 
    del_txt = (TextView) findViewById(R.id.del_txt); 

    del_txt.setOnClickListener(new OnClickListener() 
{ 
    @Override 
    public void onClick(View v) 
    { 
     "code for delete selected items" 

    } 
}); 
     } 
      public OrderAdapter(Context context, int row_layout, 
     ArrayList<x_obj> items) { 
    super(context, row_layout, items); 
    this.items = items; 
} 

@Override 
public View getView(final int position, View convertview, ViewGroup parent) { 
    View v = convertview; 

    LayoutInflater vi = (LayoutInflater)      
      getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    v = vi.inflate(R.layout.row_layout, null); 
    final x obj = items.get(position); 

     TextView txt_name = (TextView) v.findViewById(R.id.txt_name); 
     CheckBox checkBox1=(CheckBox)findViewById(R.id.checkBox1); 
      if(checkBox1.isChecked()) 
      { 
      new_al.add(x_list.get(position));//add the element into arraylist 

       } 
       btn_clear.setOnItemClickListener(new Listener() 
       { 

      public void onClick(View v) 
      { 

      x_list.clear(); 
      x_list.add(new_al); 
      } 
      } 
       ); 
       notifyDataSetChanged(); 
      return v; 

       } 
        } 

這只是一個exmple.there可以在一些places.please的語法錯誤檢查。

+0

我認爲所有你需要做的就是添加一個按鈕來清除列表中的所有選中的項目 –

+0

ia m使用刪除文本清除list..del_txt.setOnClickListener(new OnClickListener() { @Override 公共無效的onClick(視圖v) } { 「爲刪除所選項目碼」}); – learner

0

試試這個在適配器中,我通過擴大BaseAdapter使用它:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

final ViewHolder holder; 

    if (convertView == null) { 
     holder = new ViewHolder(); 
     convertView = inflater.inflate(R.layout.row1, null); 

     holder.checkbox = (CheckBox) convertView 
       .findViewById(R.id.add_food_item); 
     convertView.setTag(holder); 

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

    final ItemInList element = list.get(position); 

    holder.checkbox.setChecked(element.isSelected()); 
    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
         boolean isChecked) { 
        element.setSelected(buttonView.isChecked()); 

        if (element.isSelected()) { 
         array.add(element.getName()); 
        } else { 
         if (position < array.size()) 
          array.remove(position); 
        } 
       } 
      }); 

    return convertView; 
}