3

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

            }
        };
Community
  • 1
  • 1
Emilla
  • 484
  • 1
  • 10
  • 23

3 Answers3

2

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     
Nitin
  • 1,966
  • 4
  • 22
  • 53
  • just copypaste the code i have written – Nitin Jul 09 '12 at 11:02
  • Nitin Thank you very much.. i solved the problem. really "((BaseAdapter)yourActivity.this.lv.getAdapter()).notifyDataSetChanged();" helped me much thanks to you :) – Emilla Jul 09 '12 at 11:44
  • if this answer helped you , you can accept it as right answer @Emilla – Nitin Jul 09 '12 at 12:22
1

this example helps me to fix this :D

Boe-Dev
  • 1,585
  • 2
  • 14
  • 26
1

Here is the easy and nice tutorial for you. In this code when on click event try hiding the button

Btn01.setOnClickListener(new OnClickListener(){

 public void onClick(View v) {

//Toast.makeText(SearchUser.this, "Button 1 "+listview_arr[position], Toast.LENGTH_SHORT).show();
Btn01.setvisibility(view.Gone);
}
});

Try like this. Hope its helpful to you. Good Luck

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
  • i have already tried something like that. But some of other buttons also disappered and furthermore when i scroll down or up the list the buttons again appeared :( – Emilla Jul 09 '12 at 08:39
  • I am sure if you try that one you will definitely get your result. if not after button hideen call list.notifyDataSetChanged(); and also you must extends listactivity – Ram kiran Pachigolla Jul 09 '12 at 12:09