【发布时间】:2023-04-02 15:35:02
【问题描述】:
我目前正在使用 Lazy Adapter 将来自 Web 服务器“A”的数据解析到 Listview 中。 Each row has a button that when selected sends the data in that row to web server "B" using a separate AsyncTask class.我在带有 onTaskComplete 的 asynctask 上使用回调来更新活动。效果很好,我得到了服务器的正确响应。
我的问题是如何根据服务器的响应更新按钮图像。因此,例如,如果来自服务器的响应等于 1,则更新按钮图像,否则如果响应等于 0,则不更改按钮图像。我只是不确定如何将活动的响应传递给适配器以更新按钮图像。任何建议将不胜感激。
我的代码:
适配器:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
RowHolder holder = null;
if (convertView == null) {
vi = inflater.inflate(R.layout.group_view, null);
holder = new RowHolder();
holder.a_refid = (TextView) vi.findViewById(R.id.a_refid);
holder.a_a_name = (TextView) vi.findViewById(R.id.a_name);
holder.a_location = (TextView) vi.findViewById(R.id.a_location);
holder.checkin = (ImageView) vi.findViewById(R.id.a_checkin);
vi.setTag(holder);
} else {
holder = (RowHolder) vi.getTag();
}
HashMap<String, String> locList;
locList = data.get(position);
holder.a_refid.setText(locList.get(MainActivity.KEY_REFID));
holder.a_name.setText(locList.get(MainActivity.KEY_NAME));
holder.a_location.setText(locList.get(MainActivity.KEY_LOCATION));
HashMap<String, String> details = dbh.getUserDetails();
final String useruid = details.get("uid");
Log.d("UID", useruid);
final HashMap<String, String> finallocList = locList;
holder.a_refid.setTag(finallocList.get(MainActivity.KEY_REFID));
holder.a_name.setTag(finallocList.get(MainActivity.KEY_REFID));
holder.a_location.setTag(finallocList.get(MainActivity.KEY_REFID));
//Check In Button
holder.checkin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Check-in to: " + finallocList.get(MainActivity.KEY_NAME));
builder.setItems(new CharSequence[]{"Check-In", "Anonymous Check-In",
"Cancel"},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
new
AcheckPro(activity).execute(finallocList.get(MainActivity.KEY_REFID), useruid,
finallocList.get(MainActivity.KEY_NAME),
finallocList.get(MainActivity.KEY_NAME));
Log.d("Check In Process", String.valueOf(useruid));
break;
case 1:
Toast.makeText(activity, "button 2 clicked",
Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(activity, "button 3 clicked",
Toast.LENGTH_SHORT).show();
break;
}
}
});
builder.create().show();
}
});
return vi;
}
AsyncTask 类
public class AcheckPro extends AsyncTask<String, Void, JSONObject> {
private Activity activity;
private ProgressDialog dialog;
private AsyncTaskCompleteListener callback;
public AcheckPro(Activity act) {
this.activity = act;
this.callback = (AsyncTaskCompleteListener)act;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(activity);
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
String refid = params[0];
String uid = params[1];
String name = params[2];
String location = params[3];
JFunctions jFunctions = new JFunctions();
JSONObject json = userFunction.checkInUser(refid, uid, name, location);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
if (null != dialog && dialog.isShowing()) {
dialog.dismiss();
}
Log.d("JSON Object", String.valueOf(json));
callback.onTaskComplete(json);
}
}
【问题讨论】:
-
你能分享你从服务器获取数据时写的代码吗?
-
@Carbongixxer 分享您的代码...以便我们为您提供帮助
-
@PsyGik 添加了我的代码
标签: android android-listview android-asynctask android-arrayadapter