0

Button views visibility is supposed to change onClick, but when view recycled, the said view comes up null. Seems to be when state of following item visibility is different than its previous in list. Here is relevant Java:

class OpenChavrutaAdapter extends ArrayAdapter<HostSessionData> {


private Context mContext;
private static final String LOG_TAG = 
OpenChavrutaAdapter.class.getSimpleName();
HostSessionData hostSessionItemData;

public OpenChavrutaAdapter(Context context, ArrayList<HostSessionData> 
hostSessionArrayList) {
    super(context, 0, hostSessionArrayList);
    this.mContext = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    //get data item for position
    final ViewHolder viewHolder;
    View listItemView = convertView;
    Boolean hostListItemView = true;
    String userId = UserDetails.getmUserId();
    final HostSessionData hostLookup = getItem(position);
    final String hostId = hostLookup.getmHostId();


    if (listItemView == null) {
        viewHolder = new ViewHolder();

        //sets correct listItemView based on Caller's Context
        if (hostId.equals(userId) && mContext == mainActivityContext) {
            hostListItemView = true;
            listItemView = 
LayoutInflater.from(
getContext()).inflate(R.layout.hosting_chavrutas_list_item, parent, false); 

} else {
    //adapter called from HostSelect.class
    listItemView = 
LayoutInflater.from(getContext()).inflate(R.layout.open_host_list_item, 
parent, false);
    hostListItemView = false;
}           
//cache the viewHoslder object inside the fresh view
        listItemView.setTag(viewHolder);

    } else {
        //view is already been populated
        hostListItemView = false;
        viewHolder = (ViewHolder) listItemView.getTag();
    }
    final HostSessionData hostSessionDatas = getItem(position);

    if (hostListItemView) {
        String idOfConfirmedUser = hostLookup.getmConfirmed();
        String request1NullCheck = hostLookup.getMchavrutaRequest1();
        String request2NullCheck = hostLookup.getMchavrutaRequest2();
        String request3NullCheck = hostLookup.getMchavrutaRequest3();

Below is when throws NPE when trying to setVisibility!! Seems to only happen when "pendingRequest1" Button linear layout view is in a different state than the preceeding view that is populated. Please...some insight!

        if (request1NullCheck != null && 
hostLookup.getMchavrutaRequest1().length() > 5) {
            String chavrutaRequestIdOne = hostLookup.getMchavrutaRequest1();
            viewHolder.pendingRequest_1.setVisibility(View.VISIBLE);

            if( idOfConfirmedUser.equals(chavrutaRequestIdOne)){  
viewHolder.confirmRequest_1.setBackgroundColor(Color.parseColor("#10ef2e"));
            }
        }else{viewHolder.pendingRequest_1.setVisibility(View.GONE);}
        notifyDataSetChanged();
    }
    return listItemView;
}

1 Answer 1

0

The problem is you are using two different layouts. When the view is recycled there is a chance that it could be either of the two, and you don't know which one you will get. The code that inflates the view is skipped when convertView is not null, hence the NPE.

The good news is that there is a simple way to solve this with the BaseAdapter class.

The methods you need to override in your adapter are getItemViewType and getViewTypeCount.

This will help your adapter know how to pass none null views for recycling to your getView method.

1
  • 1
    Solved!..I tried to monitor the active viewType by the bool "hostListItemView" but that did not change when views recycled! Used getItemViewType and that worked perfectly! Thanks
    – sleethma
    Commented Sep 20, 2017 at 21:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.