2011-10-12 52 views
0

如何使用我創建的自定義ListAdapter刷新ListActivity的內容?我在arrayadapter中有一個調用「notifyDataSetChanged();」的方法。這是行不通的。在這個網站上沒有任何相關的解決方案。下面的代碼迄今:如何獲得此ListActivity更新信息?

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

}

回答

1

我還沒有看到notifyDataSetChanged()做任何事情的情況。我剛剛根據最新信息獲得了一個新適配器,並更改了ListView上的滾動位置,使其顯示爲簡單更新。

0

您不應該直接覆蓋數據數組,而應使用ArrayAdapter提供的clear/insert/add/addAll/remove方法。

+0

我找了他們,但找不到他們。我的ArrayAdapter的子類完全不能識別它們。 – user967514

+0

這是奇怪的 - 添加,刪除清除和插入已經可用,因爲API級別1,addAll只有自11。 –