Hello sir, I have Arraylist into bean class@Line 1 Below. I have mainActivity class @ Line 2 Below where i set data to listview using myAdapter(CustomAdapter class). each row has one textview and checkbox. i want to do when checkbox is checked i want to obtain that checkbox position so i can remove corresponding textview so, i used below l1.remove(i) it' s work find but what happen i have to click on listview item to remove the item but want to remove iwhen checkbox is checked or clicked i want to remove corresponding item. thanks.
Line 1>>> public class Bean {
static ArrayList<
String> getList(){
ArrayList<String> l1=new ArrayList<String>();
l1.add("Laptop");
l1.add("Desktop");
l1.add("Pendrive");
return l1;
}
}
//////////////////////////////////////////////
Line 2>> public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> l1=Bean.getList();
final ListView l2=(ListView)findViewById(R.id.listView1);
Button b1=(Button)findViewById(R.id.button1);
final myAdapter1 adapter = new myAdapter1(this, l1);
l2.setAdapter(adapter);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SparseBooleanArray boolArray=l2.getCheckedItemPositions();
for(int i=0;i<l1.size();i++)
{
if(boolArray.valueAt(i)==true)
{
l1.remove(i);
}
}
adapter.notifyDataSetChanged();
}
});
}
}
3>> public class myAdapter1 extends BaseAdapter
{
ArrayList<String> listItem;
Activity activity;
public myAdapter1(Activity activity, ArrayList<String> l1) {
// TODO Auto-generated constructor stub
listItem=l1;
this.activity=activity;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listItem.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public class ViewHolder
{
TextView txtViewItem;
CheckBox chk;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder item;
LayoutInflater inflater = activity.getLayoutInflater();
if(view==null)
{
view = inflater.inflate(R.layout.activity_main, null);
item = new ViewHolder();
item.txtViewItem = (TextView) view.findViewById(R.id.textView1);
item.chk=(CheckBox)view.findViewById(R.id.checkBox1);
view.setTag(item);
}
else
{
item = (ViewHolder) view.getTag();
}
item.txtViewItem.setText(listItem.get(position));
return view;
}
}