2012-08-15 63 views
3

我有一個ListActivity和一個擴展了BaseAdapter的自定義適配器。Android刷新來自BaseAdapter的ListActivity

在ListView的每一行上,都有一個按鈕和幾個視圖。 onClickListener在適配器中聲明。

按鈕被點擊後,我如何才能刷新列表視圖

下面是代碼

列表活動:

......... 
super.onCreate(savedInstanceState); 


here im reading content from a file and then creating adapter with that content 

    adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray); 
    setListAdapter(adapter); 
.......... 

適配器:

....... 

public class UvArrayAdapter extends BaseAdapter { 
private Context mContext; 
private LayoutInflater linflater; 
private TextView txt_1, txt_2, txt_3; 
private ProgressBar pr1; 
private String[] str1; 
private String[] str2; 
private String[] str3; 
private Integer[] int1; 

private class OnItemClickListener implements OnClickListener{  
    private int mPosition; 
    OnItemClickListener(int position){ 
     mPosition = position; 
    } 
    @Override 
    public void onClick(View arg0) { 
     Log.v("TAG", "onItemClick at position" + mPosition);   
    }  
} 



public UvArrayAdapter(Context context, String[] s1, String[] s2, Integer[] i1) { 
mContext = context; 
str1 = s1; 
str2 = s2; 
int1 = i1; 
linflater = LayoutInflater.from(context); 
} 

@Override 
public int getCount() { 
return str1.length; 
} 

@Override 
public Object getItem(int arg0) { 
return str1[arg0]; 
} 

@Override 
public long getItemId(int arg0) { 
return arg0; 
} 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

if (convertView == null) { 

    convertView = linflater.inflate(R.layout.uv_rowlayout, null); 

} 
convertView.setOnClickListener(new OnItemClickListener(position)); 
txt_1 = (TextView) convertView.findViewById(R.id.textView1); 
pr1 = (ProgressBar) convertView.findViewById(R.id.progressBar1); 
pr1.setProgress(int1[position]); 
txt_1.setText(str1[position]); 

txt_2 = (TextView) convertView.findViewById(R.id.textView2); 
txt_2.setText(str2[position]+"mV"); 
Button btn = (Button) convertView.findViewById(R.id.button1); 
btn.setOnClickListener(new OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
     Log.v("onClickListener", "button - clicked at position "    position); 

     do some changes in mentioned file and after that i need to update the list view with new data 
      }}); 




return convertView; 

} 



} 
+1

重複問題:http://stackoverflow.com/questions/11972259/how-can-i-refresh-the-list-after-changes – Budius 2012-08-15 15:48:14

回答

2

使用notifyDataSetChanged()

adapter = new UvArrayAdapter(this, strarray3, strarray2, intarray); 
setListAdapter(adapter); 
UvArrayAdapter.notifyDataSetChanged(); 

// To make the adapter aware of the changes 
+0

再次同樣的錯誤不能使靜態參考從類型BaseAdapter的非靜態方法notifyDataSetChanged() – pedja 2012-08-15 15:52:56

4
adapter.notifyDataSetChanged(); // to notify the adapter that your data has been updated 

注:如果你得到任何錯誤與上面的線,你可以試試下面的代碼(其中mListView是我的ListView對象的名稱)

​​

另一種方法是無效該列表,以便它再次從更新的數據集重新繪製。從22分鐘前

mListView=(ListView) findViewById(R.id.MyListView); 
mListView.invalidate(); 
+0

does not work,view does not update – pedja 2012-08-15 15:59:32

+0

嘗試使其無效。請檢查我更新的答案。 – Swayam 2012-08-15 16:05:23

+1

請給我一個例子,我不知道如何實現,在我的代碼 – pedja 2012-08-15 16:14:15