简体   繁体   English

具有不同布局的ListView-更改TextView中的文本

[英]ListView with different layouts - change Text in TextView

I have a custom ListView with two elemnts. 我有一个带有两个元素的自定义ListView。

    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. 但是,如果我想在TextView中使用setText,则会收到NullPointerException。

holder.txtSoundGewaehlt.setText(sound); holder.txtSoundGewaehlt.setText(sound); is working fine but holder.txtOnOff.setText("Test"); 工作正常,但是holder.txtOnOff.setText(“ Test”); crashes. 崩溃。

How can I change the Text of txtOnOff? 如何更改txtOnOff的文本?

Thanks 谢谢

The assumption that will get two null convertView is wrong. 假设将获得两个null的convertView是错误的。 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. 您将获得一个null的convertView,它可能与您在位置0充气的那一个相对应。您可以做的是将所有视图放在一个布局中,并根据位置更改组件的可见性。 Another solution could be to override getViewTypeCount() and getItemViewType() , this way you can have a number of null convertView s equal to getViewTypeCount() 另一种解决方案是重写getViewTypeCount()getItemViewType() ,这样,您可以使null的convertView数量等于getViewTypeCount()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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