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

标签 java android listactivity android-arrayadapter

如何使用我创建的自定义 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 上的滚动位置,使其看起来只是更新了。

关于java - 如何获取此 ListActivity 来更新信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746126/

相关文章:

java - 需要帮助在 java 中按下多个键

android - ListActivity:在将数据放入行之前更改来自 SQLite 的数据

java - Android,从sqlite检索数据并显示在自定义列表中的正确方法

java - Android 中 SQLite 数据库的安全性

java - 用于转换不可变列表的 Guava 单线

android - 使用挂载命令在 SnapdragonAPQ8074 中挂载 USB 驱动器

android - 如何判断 Play Integrity 判决中的证书 SHA-256 摘要是否有效?

android - java.lang.IllegalStateException : The content of the adapter has changed but ListView. ...尽管调用了 notifydatasetchanged()

java - char 数据类型并返回字母 v 数字

java - 保存 HashMap 共享首选项