我有一個活動類,它調用我的自定義適配器類,它擴展了基礎適配器。在我的getView
方法中,我有一個button.setOnClickListener
,其中有一個線程可以下載數據。我想在用戶點擊時將按鈕文本設置爲「下載」,然後在下載完成後,將按鈕文本設置爲「完成」。我怎樣才能做到這一點?從getView內的線程更新視圖?
public class MyActivity extends Activity {
//some code here
private void setAdapter(ArrayList arrayList) {
listView.setAdapter(new UserAdapter(context,arrayList));
}
}
public class UserAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
holder.button = (Button) view.findViewById(R.id.button);
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try{
//set button text to "downloading"
//establish an http connection and download data
//after download done, if successfull set button text to downloaded
//if download failed, set button text to failed.
} catch (Exception exception) {
}
}
}
).start();
}
});
return view;
}
private class Holder {
private Button button;
}
}
您將很難用這種方法。如果按鈕在下載過程中滾動屏幕,視圖會被回收,然後被重用到ListView中的新行? –