Khaled Rostampour Khaled Rostampour - 5 months ago 11
Android Question

How change text of button after some code execute when button click

I have a list of users in list view in every row I have button to send friend request to that user.
now when I click button to send request, function sendRequest executed and if execute successfully I want to change text button to something else:

Here is sendRequest function:

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 is a String that keep return value from server.

This is On click listener for button:

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


As you see I want change text when res equal "ok" .
In my code when I click button and sendRequest execute successfully text doesn't change but if I click it again it change because res keep "ok" for later.

Now my question is how to get res value after code execute?

update:

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


and:

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

}

Answer

Send Request is executed in another thread it may be executed after this line

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

So you want to override onPostExecute in your activity

Your code will be like that:

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

            }
        });