0

when i click the listview item, that particular item button is Visible. when i scroll the listview the buttons are gone. how to implement this functionality. what is the wrong in this please help me. Thanks

Here is My Code:

MainActivity.java

public class MainActivity extends Activity {

    ListView lv;
    SampleAdapter sa;
    ArrayList<String> as=new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<20;i++)
        {
            as.add(String.valueOf(i));
        }
        lv=(ListView)findViewById(R.id.listView1);
        sa=new SampleAdapter(MainActivity.this);
        lv.setAdapter(sa);

        }
    class SampleAdapter extends BaseAdapter
    {

        Context ctx;
        Button b;
        TextView tv;
        LayoutInflater lin;
        public SampleAdapter(Context ct) {
            this.ctx=ct;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return as.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int pos, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            lin=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
            arg1=lin.inflate(R.layout.li, null);
            tv=(TextView)arg1.findViewById(R.id.textView1);
            b=(Button)arg1.findViewById(R.id.button1);
            tv.setText(as.get(pos));
            b.setText(as.get(pos));
            arg1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Button c=(Button)v.findViewById(R.id.button1);
                    c.setVisibility(View.VISIBLE);
                }
            });
            return arg1;
        }

    }
    }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

li.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        android:text="Button" />

</LinearLayout>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
srinu
  • 266
  • 1
  • 5
  • 18
  • 1
    but here your button visibility you set `android:visibility="invisible"` and you said _particular item button is Visible_? simple remove `android:visibility="invisible"` – M D Apr 10 '14 at 12:02

2 Answers2

1

You have

android:visibility="invisible"

This makes the button not visible.

And then you have

c.setVisibility(View.VISIBLE);

You need to understand that listview recycles views. So when you scroll your Buttons become invisible because the views are recycled.

Check the below link

How ListView's recycling mechanism works

Well check this example which is very similar to yours

ListView subobject clickable confilct

TO fix you can use a Model class

Model class

public class Model {
int visibility;
 String value;


public int getVisibility() {
    return visibility;
}
public void setVisibility(int visibility) {
    this.visibility = visibility;
}
public String getValue() {
    return value;
}
public void setValue(String value) {
    this.value = value;
}

}

MainActivity.java

public class MainActivity extends Activity {

    ListView lv;
    SampleAdapter sa;
    final int visible1 = View.INVISIBLE;
    final int visible2 = View.VISIBLE;
    ArrayList<Model> as=new ArrayList<Model>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        for(int i=0;i<20;i++)
        {
            Model model  = new Model();
            model.setValue(String.valueOf(i));
            model.setVisibility(visible1);
            as.add(model);
        }
        lv=(ListView)findViewById(R.id.listView1);
        sa=new SampleAdapter(MainActivity.this);
        lv.setAdapter(sa);

        }
    class SampleAdapter extends BaseAdapter
    {

        Context ctx;
        Button b;
        TextView tv;
        LayoutInflater lin;
        public SampleAdapter(Context ct) {
            this.ctx=ct;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return as.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int pos, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            lin=(LayoutInflater)ctx.getSystemService(LAYOUT_INFLATER_SERVICE);
            arg1=lin.inflate(R.layout.fg, null);
            tv=(TextView)arg1.findViewById(R.id.textView1);
            b=(Button)arg1.findViewById(R.id.button1);
            tv.setText(as.get(pos).getValue());
            b.setText(as.get(pos).getValue());
            arg1.setTag(pos);
            b.setVisibility(as.get(pos).getVisibility());
            arg1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    int pos = (int) v.getTag();
                    Model model = as.get(pos);
                    model.setVisibility(visible2);
                    SampleAdapter.this.notifyDataSetChanged();

                }
            });
            return arg1;
        }

    }
    }

Snap

You can scroll up and down and the state remains

enter image description here

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Remove this in your adapter:

arg1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Button c=(Button)v.findViewById(R.id.button1);
                    c.setVisibility(View.VISIBLE);
                }
            });

and this in your li.xml

android:visibility="invisible"

You have code that makes the button invisible by defailt in your li.xml and onItemClick in your adapter your making it visible

Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117