在android适配器中更新运行时的按钮值

时间:2014-02-03 13:13:04

标签: android button android-listview listadapter

考虑我的android listview中的项目列表。

如果我必须更改我的第3个项目的值意味着它获得最后一个项目值。

我的代码有什么问题?请给我一个解决方案。

我在我的适配器文件中使用了belo代码:

    public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;

    if(convertView==null) {
        vi = inflater.inflate(R.layout.list_profile_griditem, null);
        followphotobtn = (Button)vi.findViewById(R.id.followphoto);


 HashMap<String, String> Order = new HashMap<String, String>();
    Order = data.get(position);
    final String photofollowstatus = Order.get(Profile.TAG_PHOTO_FOLLOW_STATUS);
     if(photofollowstatus.equalsIgnoreCase("Following")){
        followphotobtn.setText("UnFollow");
            System.out.println("FollowBtn"+" "+"UnFollow");
     }
     else {
        followphotobtn.setText("Follow");
                   System.out.println("FollowBtn"+" "+"Follow");
        }

它运行良好......我们正确地获得输出。

02-03 19:44:57.276: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.276: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.284: I/System.out(19871): FollowBtn UnFollow
02-03 19:44:57.292: I/System.out(19871): FollowBtn Follow

但是我没有在onclicklister中得到正确的结果:

  followphotobtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {


             if(photofollowstatus.equals("Following")){
                 new UnFollowPhoto().execute(photo_id);

            }
            else {

                 new FollowingPhoto().execute(photo_id);


                 }
                    }
        });
        });
    return vi;
 }

  class FollowingPhoto extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //showDialog(DIALOG_LOADING);
    }
    protected String doInBackground(String... args) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://dev.xxx.com/xxx/client/follow.php");
        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            System.out.println("photo_id"+" "+args[0]);
            nameValuePairs.add(new BasicNameValuePair("userid", LoginPage.userid));
            nameValuePairs.add(new BasicNameValuePair("photo_id", args[0]));
            nameValuePairs.add(new BasicNameValuePair("photofollow_status", "Following"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            result_status = EntityUtils.toString(entity);
            //res = response.toString();
            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }
            return result_status;

            }


    protected void onPostExecute(String file_url) {
        super.onPostExecute(file_url);
        //dismissDialog(DIALOG_LOADING);
        Toast.makeText(activity.getApplicationContext(), "Following", Toast.LENGTH_LONG).show();
        followphotobtn.setText("UnFollow");
        Profile.myphotolistAdapter.notifyDataSetChanged();
        followphotobtn.setText("UnFollow");
        System.out.println(followphotobtn.getText().toString());
        }
}

但我得到的输出是:

  02-03 19:45:10.534: I/System.out(19871):  UnFollow

但单击此按钮时,此适配器文件中的值不会更新..

但我已经编写了如下代码:

protected void onPostExecute(String file_url) {
        super.onPostExecute(file_url);
        //dismissDialog(DIALOG_LOADING);
        Toast.makeText(activity.getApplicationContext(), "Following", Toast.LENGTH_SHORT).show();
        followprofilephotobtn.setText("UnFollow");
        Intent intent = new Intent(activity,Profile.class);
        activity.startActivity(intent);
        }

重新加载活动后,该值会更改。但我需要更新值而不重新加载活动。

我的代码有什么问题?为什么我要面对这个问题?

请给我一个想法和建议?

2 个答案:

答案 0 :(得分:0)

您必须以这种方式绑定适配器类中的按钮单击侦听器

public View getView(int position, View convertView, ViewGroup parent){
        View view = super.getView(position, convertView, parent);
        Button button = (Button)view.findViewById(R.id.followphoto);
        button.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                Log.i(TAG, "clicked");
            }
        }); 
        return view;
 }

答案 1 :(得分:0)

在getView方法

中为您的按钮设置标签
followphotobtn.setTag(position);

然后正如@Mukesh在onClickListener中所说的那样使用getTag来获取被点击的项目的状态。请参阅下面的类似链接here.