按钮点击后如何在执行某些代码后更改按钮文本

时间:2016-06-16 21:50:33

标签: android return-value

我在列表视图中有一个用户列表,每行都有按钮向该用户发送好友请求。 现在,当我点击按钮发送请求时,执行sendRequest函数,如果执行成功,我想将文本按钮更改为其他内容:

这是sendRequest函数:

function cnt(str){
  var a = {};//empty object
  str.replace(/\b(\D+)(\d+)/ig, 
              function(m, nm, val){
                //m - whole match
                //nm is (\D+), val is (\d+)
                // \d - digits 0 - 9, \D - non-digits, \b - word boundary 
                a[nm] = a[nm] || [];//in case there are cars38 and cars125
                a[nm].push(val);
                return m;// do not change str
              });
  return a;
}

var a = cnt("books23 birds47 cars38 cars125");
console.log(a); // Object { books:["23"], birds:["47"], cars:["38", "125"]}

res是一个保持服务器返回值的String。

这是On按钮的单击侦听器:

 public class sendRequest extends AsyncTask {

        private String uid, f_id;
        public sendRequest(String user_id, String friend_id) {
            f_id = friend_id;
            uid = user_id;
        }

        @Override
        protected Object doInBackground(Object... arg0) {
            // TODO Auto-generated method stub
            try {
                String data = URLEncoder.encode("id", "utf8") + "=" + URLEncoder.encode(uid + "", "utf8");
                data += "&" + URLEncoder.encode("friend_id", "utf8") + "=" + URLEncoder.encode(f_id + "", "utf8");
                URL link = new URL(AppConfig.URL_Add_user);
                URLConnection connect = link.openConnection();

                //send data
                connect.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
                wr.write(data);
                wr.flush();
                BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                res = sb.toString();
            } catch (Exception e) {
                res = e.toString();
            }
            return res;
        }

        @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (res.equals("ok")) {
                Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();

            } else if (res.equals("exist")) {
                Toast.makeText(context, "You Already Send Request", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, res, Toast.LENGTH_LONG).show();
            }

        }
    }

如你所见,当res等于" ok" 。 在我的代码中,当我单击按钮并且sendRequest执行成功时,文本没有改变但是如果我再次单击它会改变,因为res keep" ok"以后。

现在我的问题是如何在代码执行后获得res值?

更新

holder.btnAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                new sendRequest(user_id, userModel.getId()).execute();
                if (res.equals("ok")) {
                    holder.btnAddFriend.setText("Request Send");
                }
            }
        });

 holder.btnAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new sendRequest(user_id, userModel.getId()){
                    @Override
                    protected void onPostExecute(String result) {
                        // TODO Auto-generated method stub
                        super.onPostExecute(result);
                        if (result.equals("ok")) {
                            Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
                            holder.btnAddFriend.setText("Request Send");


                        } else if (result.equals("exist")) {
                            Toast.makeText(context, "You Already Send Request", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(context, result, Toast.LENGTH_LONG).show();
                        }

                    }
                }.execute();
            }
        });

2 个答案:

答案 0 :(得分:1)

发送请求在另一个线程中执行,可以在此行之后执行

if (res.equals("ok")) {
                    holder.btnAddFriend.setText("Request Send");
                }

因此,您希望覆盖活动中的onPostExecute

您的代码将是这样的:

holder.btnAddFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                new sendRequest(user_id, userModel.getId()){
 @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (res.equals("ok")) {
                Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
                    holder.btnAddFriend.setText("Request Send");


            } else if (res.equals("exist")) {
                Toast.makeText(context, "You Already Send Request", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, result, Toast.LENGTH_LONG).show();
            }

        }
}.execute();

            }
        });

答案 1 :(得分:0)

你需要一个Handler和一个Runnable,如下所示:

Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
                handler.post(new Runnable(){
                    public void run() {
                       holder.btnAddFriend.setText("Request Send");
                }
            });
            }
        }
    };
new Thread(runnable).start();

执行该操作而不是仅仅调用

holder.btnAddFriend.setText("Request Send");