异步任务列表视图后更新按钮视图

时间:2014-01-31 06:14:09

标签: android android-listview android-asynctask android-arrayadapter

我目前正在使用Lazy Adapter将数据从Web服务器“A”解析为Listview。每行都有一个按钮,当选中该按钮时,使用单独的AsyncTask类将该行中的数据发送到Web服务器“B”。我在asynctask上使用onTaskComplete回调来更新活动。哪个工作正常,我从服务器得到正确的答复。

我的问题是如何根据服务器的响应更新按钮图像。因此,例如,如果服务器的响应等于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);

}
}

1 个答案:

答案 0 :(得分:0)

你可以通过这个逻辑

来做到这一点
public class MainActivity extends Activity implements OnClickListener{
Button myButton;
boolean flag = false;
RelativeLayout rl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rl = (RelativeLayout) findViewById(R.id.relative);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) new LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        myButton = new Button(this);
        myButton.setOnClickListener(this);

        if(flag){
            myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.record_button_pressed));
        }else{
            myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.record_button_normal));
        }

        rl.addView(myButton,params);

    }

你可以通过它的构造函数将响应传递给AdapterClass。