繁体   English   中英

如何获取此ListActivity来更新信息?

[英]How do I get this ListActivity to update the info?

如何使用我创建的自定义ListAdapter刷新ListActivity的内容? 我在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){
}

}

我还没有看到notifyDataSetChanged()做任何事情的情况。 我刚刚根据最新信息获得了一个新的适配器,并更改了ListView上的滚动位置以使其看起来像是刚刚更新的。

您不应该直接覆盖数据数组-而是使用ArrayAdapter提供的clear / insert / add / addAll / remove方法。

暂无
暂无

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

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