Possible Duplicate:
Handling button event in each row of Listview issue
I am working on an android app. I am supposed to add a button in each row of ListView and when I click any button I need the button should be removed. However when I do so, button is removed but some other buttons in other positions also removed. Furthermore when i scroll down and up the ListView these buttons which I clicked and removed again appear in the list. Could you please show me an exact solution or a completed piece of code? Thank you...
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
// LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listxml, null);
}
rawTextView = (TextView) convertView.findViewById(R.id.textView1);
rawTextView.setText("Text "+innerClassBookArray[position]);
final LinearLayout lnr=(LinearLayout)convertView.findViewById(R.id.layoutforbtn);
final Button btn = new Button(getBaseContext());
btn.setText("MyButton");
btn.setId(position);
lnr.addView(btn);
btn.setOnClickListener(myOnBtnClickListener);
return convertView;
}
private OnClickListener myOnBtnClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int posit = bookListView.getPositionForView((View) v.getParent());
Toast.makeText(getApplicationContext(), Integer.toString(posit), Toast.LENGTH_LONG).show();
}
};
You can do it like this make a Array-list where every element as "0" (Arraylist<String> my=new ArrayList<String>(); Size of Arraylist should be according to the size of column in list view)and on button click set the "1"
at the position of like my.set(position,"1"); and then notify the list view. also see if condition in the below code
for example
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
// LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listxml, null);
}
rawTextView = (TextView) convertView.findViewById(R.id.textView1);
rawTextView.setText("Text "+innerClassBookArray[position]);
Button button =(Button)findViewById(R.id.yourid);
if(my.get(position).equals("1")){
button.setVisiblity(View.INVISIBLE);
}
final LinearLayout lnr=(LinearLayout)convertView.findViewById(R.id.layoutforbtn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
my.set(position,"1");
((BaseAdapter)yourActivity.this.lv.getAdapter()).notifyDataSetChanged();
}
});
return convertView;
}
I think this will definitely help you any question ask me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With