簡體   English   中英

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

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

我有一個帶有兩個元素的自定義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();
}

一切正常。 但是,如果我想在TextView中使用setText,則會收到NullPointerException。

holder.txtSoundGewaehlt.setText(sound); 工作正常,但是holder.txtOnOff.setText(“ Test”); 崩潰。

如何更改txtOnOff的文本?

謝謝

假設將獲得兩個null的convertView是錯誤的。 您將獲得一個null的convertView,它可能與您在位置0充氣的那一個相對應。您可以做的是將所有視圖放在一個布局中,並根據位置更改組件的可見性。 另一種解決方案是重寫getViewTypeCount()getItemViewType() ,這樣,您可以使null的convertView數量等於getViewTypeCount()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM