Как мне получить этот ListActivity для обновления информации? - PullRequest
0 голосов
/ 13 октября 2011

Как обновить содержимое ListActivity с помощью созданного мной пользовательского адаптера ListAdapter?У меня в arrayadapter метод, который вызывает «notifyDataSetChanged ();».Это не работает.Ни один из соответствующих решений на этом сайте.Вот код на данный момент:

private final Activity context;
private Message[] messages;

public RamRSSAdapter(Activity context, Message[] messages) {
    super(context, R.layout.ram_rss_row);
    this.context = context;
    this.messages = messages;
}

// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
    public ImageView imageView;
    public TextView textView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ViewHolder will buffer the assess to the individual fields of the row
    // layout

    ViewHolder holder;
    // Recycle existing view if passed as parameter
    // This will save memory and time on Android
    // This only works if the base layout for all classes are the same
    View rowView = convertView;

    //string code goes here
    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.ram_rss_row, null, true);
        holder = new ViewHolder();
        holder.textView = (TextView) rowView.findViewById(R.id.label);
        holder.imageView = (ImageView) rowView.findViewById(R.id.icon);
        rowView.setTag(holder);
    } else {
        holder = (ViewHolder) rowView.getTag();
    }

    holder.textView.setText(messages[position].getTitle());

    //code for image here
holder.imageView.setImageResource(getImageResID(getType(messages[position].getTitle())));

    return rowView;
}
private String getType(String title){
    int i1 = title.indexOf("[");
    int i2 = title.indexOf("]");
    if((i1==-1)||(i2==-1)){
        return "";
    }else{
        return title.substring(i1+1, i2);
    }
}
public void changeData(Message[] newData){
    messages = newData;
    notifyDataSetChanged();
}
/*
private int getImageResID(String type){
}

}

Ответы [ 2 ]

1 голос
/ 13 октября 2011

Мне еще предстоит увидеть случай, когда notifyDataSetChanged () что-то делает. Я только что получил новый адаптер на основе последней информации и изменил положение прокрутки в ListView, чтобы оно выглядело просто обновленным.

0 голосов
/ 13 октября 2011

Вы не должны перезаписывать массив данных напрямую - вместо этого используйте методы clear / insert / add / addAll / remove, которые предоставляются ArrayAdapter.

...