0
我正在使用ArrayAdapter在列表中顯示我的朋友。但是,每行都有兩個按鈕,通過POST請求更新朋友的狀態。Android單擊每行多個按鈕列表的監聽器
我在適配器中定義了兩個按鈕的點擊偵聽器,以及可處理更新請求的可運行對象。舉個例子:
mToggleAlert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mToggleAlert = (ToggleButton)finalRowView.findViewById(R.id.toggleAlert);
mToggleShow = (ToggleButton)finalRowView.findViewById(R.id.toggleShow);
updateStatus(finalRowView.getId(), mToggleAlert.isChecked(), mToggleShow.isChecked());
}
});
/**
* Send the updated status to the server
*/
private void updateStatus(int id, boolean alert, boolean show) {
final int finalId = id;
final boolean finalAlert = alert;
final boolean finalShow = show;
String.valueOf(mToggleAlert);
Runnable runnable = new Runnable() {
public void run() {
Message msg = handler.obtainMessage();
Bundle bundle = new Bundle();
//Post the update to the server
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.postFriendStatusUrl);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user_id",Common.retrieveAppUser(mContext).getId()));
nameValuePairs.add(new BasicNameValuePair("friend_id", String.valueOf(finalId)));
nameValuePairs.add(new BasicNameValuePair("alert_status", String.valueOf(finalAlert)));
nameValuePairs.add(new BasicNameValuePair("show_status", String.valueOf(finalShow)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
bundle.putString("result", sb.toString());
msg.setData(bundle);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
handler.sendMessage(msg);
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
我不認爲這是接近這一點,雖然一個好辦法,所以我把更新服務到它自己的AsyncTask。
我的問題是:我寧願爲實現ArrayAdapter的Activity中的兩個按鈕定義點擊偵聽器,而不是在適配器本身。我該怎麼做呢?
創建您的customadapter擴展baseadapter類,[請參閱此處](http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92) – smkrn110