Skip to content Skip to sidebar Skip to footer

Android Setvisibility For Button In Listview

I am parsing a json url inside a fragment and put the data into a listview. For each element i have a hidden button with id button1 as you can see in my xml layout.

Solution 1:

In Emi

String recording;
publicStringgetRecording() {
    return recording;
}

publicvoidsetRecording(String recording) {
    this.recording = recording;
}

In the for loop

if (obj.getString("Recordings").equals("Yes")) {
         movie.setRecording("Yes");
   }else{
         movie.setRecording("No");
   }

Remove this

Buttonb= (Button) getView().findViewById(R.id.button1);
  b.setVisibility(View.VISIBLE);

Then in adapter getView

  Button b = (Button) convertView.findViewById(R.id.button1);
  if(m.getRecording().equals("Yes"))
  b.setVisibility(View.VISIBLE);
  else
  b.setVisibility(View.INVISIBLE);

Also you should use a ViewHolder pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Solution 2:

I suppose in the xaml of the adapter you have also a button, i suggest you to initialize it in EmisiuniAdapter.java with

Buttonbutton= (Button) findViewById(R.Id.buttonName);

and than you can set the visibility property here:

// I can only image the code, replace it with the correct name/methodif(!m.getRecordings){
    //this getRecordings is the method that must give you the yes/no from objectbutton.setVisibility(View.GONE);
}

this should work, and you can remove the method you create to remove the visibility from page ;)

Solution 3:

What you need to do is hide/show your button in the adapter itself rather than in the loop.

In your bean class save the value which you got from JSON

movide.setRecordings(obj.getString("Recordings"));

and then in adapter put your logic something like,

if (m.getString("Recordings").equals("Yes")) {
    Button b = (Button) getView().findViewById(R.id.button1);
    b.setVisibility(View.VISIBLE);
}

You can modify your logic accordingly.

Solution 4:

try this ;

button.setVisibility(View.GONE);//when you want to hidebutton.setVisibility(View.VISIBILITY);//when you want to show

Solution 5:

Step1) make the default visibility of button1 to invisible.

Step2) add one "Boolean mbutVisibility" field to class "Emi" .

Step3) make true in your for loop(response one .you have mentioned above ) and save that data into arraylist

Step4) pass data to adapter and check if the boolean value if true then make it visible else its already invisible.

i have myself did this .every thing working fine

Post a Comment for "Android Setvisibility For Button In Listview"