-1

I have a custom ListView with two elemnts.

    Context mContext;
LayoutInflater inflater;
String sound;
Boolean vibrate;

public BenachrichtigungLVAdapter(Context context, String sound, Boolean vibrate) {
    mContext = context;
    inflater = LayoutInflater.from(mContext);
    this.sound = sound;
    this.vibrate = vibrate;
}

public class ViewHolder {
    TextView txtSoundWaehlen;
    TextView txtSoundGewaehlt;
    TextView txtVibrationTV;
    TextView txtOnOff;
}


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

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder = null;
    holder = new ViewHolder();
    if (view == null) {
        switch (position) {
        case 0:
            view = inflater.inflate(R.layout.soundsingle, null);
            holder.txtSoundWaehlen = (TextView) view.findViewById(R.id.Soundwaehlen);
            holder.txtSoundGewaehlt = (TextView) view.findViewById(R.id.selectedSound);
            break;
        case 1:
            view = inflater.inflate(R.layout.vibrationsingle, null);
            holder.txtVibrationTV = (TextView) view.findViewById(R.id.VibrationTV);
            holder.txtOnOff = (TextView) view.findViewById(R.id.OnOffTV);
            break;
        }
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    if(position == 0){
        holder.txtSoundGewaehlt.setText(sound);
    } else if(position == 1){
        holder.txtOnOff.setText("Test");
    }

    return view;
}

@Override
public int getCount() {
    // TODO Automatisch generierter Methodenstub
    return 2;
}

@Override
public Object getItem(int position) {
    // TODO Automatisch generierter Methodenstub
    return null;
}

public void setSound(String sound){
    this.sound = sound;
    notifyDataSetChanged();
}

Everything is working fine. But if I want to setText in TextView i get a NullPointerException.

holder.txtSoundGewaehlt.setText(sound); is working fine but holder.txtOnOff.setText("Test"); crashes.

How can I change the Text of txtOnOff?

Thanks

t18935
  • 59
  • 1
  • 6

1 Answers1

3

The assumption that will get two null convertView is wrong. You will get one null convertView, and it will probably correspond to the one that you inflate at position 0. What you can do is to put all the view's in one layout and change the visibility of the components according with the position. Another solution could be to override getViewTypeCount() and getItemViewType(), this way you can have a number of null convertViews equal to getViewTypeCount()

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • No, Do it the correct way and use getViewTypeCount() like in this example :http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row – Decoy Sep 18 '14 at 14:23
  • I was finishing to edit my answer. Still, in my opinion it does not make sense to override those methods just for 2 elements @Decoy – Blackbelt Sep 18 '14 at 14:25
  • It doesn't matter how many different views the code will be much more clear and understandable... – Decoy Sep 18 '14 at 14:27
  • 1
    I do think that's more a matter of taste, – Blackbelt Sep 18 '14 at 14:28
  • @Selvin, that's also true – Blackbelt Sep 18 '14 at 14:29
  • @Selvin There's nothing wrong with using a listview for two element, using the holder pattern is abundant – Decoy Sep 18 '14 at 14:35
  • @blackbelt if your taste is to ignore easy to use methods and reinventing the wheel than go ahead – Decoy Sep 18 '14 at 14:37
  • don't take me wrong. But in the second way you get the double of number of views you'll get in first place. Sometimes simply I can not (or I do not want to) afford it @Decoy – Blackbelt Sep 18 '14 at 14:38
  • Yes, there is... ListView adds unnecessary views... Instead holder pattern you can simply store references to those view in private class member – Selvin Sep 18 '14 at 14:39
  • Yeah ? what kind of views ? – Decoy Sep 18 '14 at 14:41
  • first ListView as itself then 2 (1 for each item) containers - layout for item ... you can use http://developer.android.com/tools/help/hierarchy-viewer.html if you want to know ... ["Would You Like to Know More?"](http://developer.android.com/training/improving-layouts/optimizing-layout.html) – Selvin Sep 19 '14 at 09:31