简体   繁体   中英

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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ler"
    >

    <!-- Thumbnail Image -->
    <com.android.volley.toolbox.NetworkImageView
        android:id="@+id/thumbnail"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp"/>

    <!-- Movie Title -->
    <TextView
        android:id="@+id/title"
        android:paddingTop="40dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/title"
        android:textColor="@color/bastru"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/desc"
        android:layout_below="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/thumbnail"

        android:textSize="@dimen/desc"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Vezi inregistrari"
        android:layout_toRightOf="@+id/thumbnail"
        android:layout_below="@+id/desc"
        android:visibility="invisible"/>
    <TextView
        android:id="@+id/link"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/title"
        android:textStyle="bold"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/cand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:textSize="@dimen/desc"
        android:textColor="@color/verde"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        />


</RelativeLayout>

Now, for every json element i have an entry called "Recordings" which has value Yes or No. If the value is Yes, it must set the button for the specific row visible.

  for (int i = 0; i < response.length(); i++) {
      try {
          JSONObject obj = response.getJSONObject(i);
          Emi movie = new Emi();

          if (obj.getString("Recordings").equals("Yes")) {
              Button b = (Button) getView().findViewById(R.id.button1);
              b.setVisibility(View.VISIBLE);
          }
      movie.setTitle(obj.getString("Titlu"));
      movie.setDesc(obj.getString("Descriere"));
      movie.setCand(obj.getString("Detalii"));
      movie.setThumbnailUrl(obj.getString("PozaModerator"));
      movie.setLink(obj.getString("iurl"));
      // adding movie to movies array
      movieList.add(movie);
      .
      .
      .

But the app always crashes with this error:

08-25 11:44:48.615  10248-10248/com.testapp.aacplay E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.testapp.aacplay.Emisiuni$2.onResponse(Emisiuni.java:105)
            at com.testapp.aacplay.Emisiuni$2.onResponse(Emisiuni.java:88)
            at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
            at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5227)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
            at dalvik.system.NativeStart.main(Native Method)

And this is the code for the adapter:

public class EmisiuniAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<Emi> movieItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public EmisiuniAdapter(Activity activity, List<Emi> movieItems) {
        this.activity = activity;
        this.movieItems = movieItems;
    }


    public int getCount() {
        return movieItems.size();
    }


    public Object getItem(int location) {
        return movieItems.get(location);
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_emisiuni, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);
        TextView link = (TextView) convertView.findViewById(R.id.link);
        TextView desc = (TextView) convertView.findViewById(R.id.desc);
        TextView cand = (TextView) convertView.findViewById(R.id.cand);



        // getting movie data for the row
        Emi m = movieItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

        // title
        title.setText(m.getTitle());

        link.setText(m.getLink());
        desc.setText(m.getDesc());
        cand.setText(m.getCand());
        return convertView;
    }

}

In Emi

String recording;
public String getRecording() {
    return recording;
}

public void setRecording(String recording) {
    this.recording = recording;
}

In the for loop

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

Remove this

  Button b = (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

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

Button button = (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/method
if(!m.getRecordings){
    //this getRecordings is the method that must give you the yes/no from object
    button.setVisibility(View.GONE);
}

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

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.

try this ;

button.setVisibility(View.GONE);//when you want to hide

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM