0

我目前使用Lazy Adapter将数据从Web服务器“A”解析到Listview中。每行都有一个按钮,当选择该按钮时,使用单独的AsyncTask类将该行中的数据发送到Web服务器“B”。我正在使用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); 

} 
} 
+0

您可以共享您从服务器获取数据时编写的代码吗? – PsyGik

+0

@Carbongixxer分享您的代码......以便我们可以为您提供帮助 – nitesh

+0

@PsyGik添加了我的代码 – Carbongixxer

回答

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); 

    } 

而且你可以通过响应阿达pterClass通过它的构造函数。

+0

这个答案没有解决特别是“我只是不确定如何将响应从活动传递到适配器更新按钮图像“。它仅仅显示了按钮在被点击时如何被改变:这与在异步回调之后更新按钮不同。 – RichieHH