2016-06-16 212 views
0

我有列表中的用戶列表中的每一行我有按鈕發送給該用戶的朋友請求。 現在當我點擊按鈕發送請求,功能sendRequest將執行,如果執行成功,我想改變文本按鈕,別的東西:按鈕點擊後如何更改一些代碼後的按鈕文本

這裏是sendRequest將功能:

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

     } 
    } 

資源是保持返回一個字符串來自服務器的值。

這是在點擊監聽按鈕:

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

正如你看到的我想更改文本時RES等於「確定」。 在我的代碼中,當我點擊按鈕併發送請求成功執行時,文本不會改變,但如果再次單擊它,它會因爲資源保留「確定」以後而更改。

現在我的問題是如何獲得代碼執行後的res值?

更新:

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

和:

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

    } 
+0

檢查這個問題的第一個答案[鏈接](http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a )。 只是一個評論 - 在Java中,類名通常以大寫字母開頭:) –

回答

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

      } 
     }); 
+0

@Khaled Rostampour試試這個,讓我知道它是否可以正常工作 –

+0

然後再次沒有任何東西從onPostExecute()返回。他的情況不會奏效。 –

+0

@TodorKostov它可能衝突的變量命名我更新我的答案現在看到它 –

0

你需要一個處理器和一個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"); 
+0

它不起作用 –

相關問題