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();
}
};