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

发布于 2024-12-09 19:19:22 字数 2024 浏览 0 评论 0原文

如何使用我创建的自定义 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){
}

}

How do I refresh the content of a ListActivity using the custom ListAdapter that I created? I have in the arrayadapter a method that calls "notifyDataSetChanged();". That does not work. Neither have any of the related solutions on this site. Here's the code thus far:

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){
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心病无药医 2024-12-16 19:19:22

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

I have yet to see a case where notifyDataSetChanged() does anything. I've just gotten a new adapter based on the latest information, and changed the scroll position on the ListView to make it appear it's simply updated.

空心↖ 2024-12-16 19:19:22

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

You should not overwrite the array of data directly- instead use the clear/insert/add/addAll/remove methods that are provided by the ArrayAdapter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文