Reputation: 1209
I have a class which has a Listview. The ListView is populated using a pattern.xml file which has a button in it. when the class is called the Button gets copied for each item in the listview. now what i want is to access those button to delete the corresponding item from the list. so how can i do that? Please help me to solve this. The code for that class is given bellow.
public class Secondscreen extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
ListView lv= (ListView) findViewById(R.id.listView1);
final Button thirdBtn = (Button) findViewById(R.id.third);
final Controller aController = (Controller) getApplicationContext();
final int cartSize = aController.getCart().getCartSize();
final ArrayList<Listitem> arrayList=new ArrayList<Listitem>();
BaseAdapter adapter= new BaseAdapter(){
@Override
public int getCount() {
// TODO Auto-generated method stub
return arrayList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public View getView(final int position, View view, ViewGroup viewgroup) {
if (view == null) {
view=inflater.inflate(R.layout.pattern, null);
}
TextView tv=(TextView) view.findViewById(R.id.nameview);
TextView tv2=(TextView) view.findViewById(R.id.pdesc);
TextView tv3=(TextView) view.findViewById(R.id.priceView);
tv.setText(arrayList.get(position).getName());
tv2.setText(""+arrayList.get(position).getPrice());
tv3.setText(arrayList.get(position).getDesc());
return view;
}
};
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
if(cartSize >0)
{
for(int i=0;i<cartSize;i++)
{
String pName = aController.getCart().getProducts(i).getProductName();
int pPrice = aController.getCart().getProducts(i).getProductPrice();
String pDisc = aController.getCart().getProducts(i).getProductDesc();
Listitem item=new Listitem(pName, pPrice, pDisc);
arrayList.add(item);
adapter.notifyDataSetChanged();
}
}
}
}
Upvotes: 0
Views: 75
Reputation: 1135
You have to specify OnClickListener
of Button
and remove Item from arrayList
in your BaseAdapter
and call notifyDataSetChanged();
@Override
public View getView(final int position, View view, ViewGroup viewgroup) {
if (view == null) {
view=inflater.inflate(R.layout.pattern, null);
}
Button button = (Button) view.findViewById(R.id.button);
//button.setTag(position);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
//Integer position = (Integer)view.getTag();
arrayList.remove(position);
adapter.notifyDataSetChanged();
}
});
// Your other views...
return view;
}
Upvotes: 1