2016-04-06 56 views
0

我有三個類,我的適配器類,投票類和acitivty,其中我的JSONrequest如下所示。問題是,你可以看到我在我的活動中調用我的JSONrequest,它會得到一個視圖列表,查看我有一個mVote textview。在我的適配器中,你也可以看到當有人按下時我有一個likeButton,我希望mVotes從0更改爲1.我從服務器獲取所有數據,因此我假設我需要提出新的請求,是否需要使新的適配器?和JSON解析方法?我該怎麼做呢?!??!我有一個視圖,並在該視圖內我有一個textview我想更新該textview?

public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{ 
private LayoutInflater mLayoutInflater; 
private ArrayList<QuestionData> data =new ArrayList<>(); 
public AdapterQuestion(Context context){ 
    //get from context 
    mLayoutInflater=LayoutInflater.from(context); 

} 
public void setBloglist(ArrayList<QuestionData> data){ 
    this.data =data; 
    notifyItemRangeChanged(0, this.data.size()); 
} 
@Override 
public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType){ 
    ViewQuestion holder=new ViewQuestion(view); 
    return holder; 
} 
@Override 
public void onBindViewHolder(ViewQuestion holder, int position) { 
    holder.answerText.setText(currentObj.getMtext()); 
    holder.answerId.setText(currentObj.getId()); 
    holder.mVotes.setText(currentObj.getVotes()); 
    holder.mLikeButton.setTag(currentObj); 
} 

@Override 
public int getItemCount() { 
    return data.size(); 
} 

public class ViewQuestion extends RecyclerView.ViewHolder{ 
    private TextView answerText; 
    private TextView answerId; 
    private TextView mVotes; 
    private LikeButton mLikeButton; 

    public ViewQuestion (View itemView){ 
     super(itemView); 
     answerText=(TextView)itemView.findViewById(R.id.answerText); 
     answerId=(TextView)itemView.findViewById(R.id.answerId); 
     mVotes=(TextView)itemView.findViewById(R.id.VoteTextView); 
     mLikeButton= (LikeButton)itemView.findViewById(R.id.heart_buttons); 

     mLikeButton.setOnLikeListener(new OnLikeListener() { 
      @Override 
      public void liked(LikeButton likeButton) { 
       Voting vote = new Voting(); 
       vote.onUpVote(answerId()); 
       System.out.print("Adapter Position"+getAdapterPosition()); 
      } 
      @Override 
      public void unLiked(LikeButton likeButton) { 
       Voting onDown=new Voting(); 
       onDown.onDownVote(answerId()); 

      } 
     }); 

    } 
    public String getVoteView(){ 
     String voteView=mVotes.getText().toString(); 
     return voteView; 
    } 
    public String answerId(){ 
     String converted=answerId.getText().toString(); 
     return converted; 
    } 
    public int convertToInt(){ 
     String converted=answerId.getText().toString(); 
     int ConvertedInt=Integer.parseInt(converted); 
     return ConvertedInt; 
    } 
} 
} 

投票

public class Voting { 
private VolleySingleton mVolleySingleton; 
private RequestQueue mRequestQueue; 
private AdapterVoteUpdate mAdapterVotes; 
private ArrayList<QuestionData> updateVotes = new ArrayList<>(); 

public void onUpVote(final String answerId) { 
    final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue(); 
    final String PUT_VOTE_UP = "url" + answerId + "url\n"; 
    StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_UP, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
     } 

    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    mrequestQueue.add(PostVoteUp); 
} 
public void onDownVote(final String answerId) { 
    System.out.println("Voted Down"); 
    final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue(); 
    final String PUT_VOTE_DOWN = "url" + answerId + "urul"; 
    StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_DOWN, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 

     } 

    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      error.printStackTrace(); 
      System.out.println("************Answer" + error + "error"); 
     } 
    }); 
    mrequestQueue.add(PostVoteUp); 
} 

JSON RequestClass

public void JsonRequestMethod(String Id) { 
    mVolleySingleton = VolleySingleton.getInstance(); 
    mRequestQueue = mVolleySingleton.getRequestQueue(); 
    final String URL_ANSWER = "url" + Id + "url"; 
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_ANSWER, (String) null, new Response.Listener<JSONArray>() { 
     @Override 
     public void onResponse(JSONArray response) { 
      mListblogs.clear(); 
      mListblogs = parseJSONResponseQuestion(response); 
      mAdapterQuestion.setBloglist(mListblogs); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      System.out.println(error); 

     } 
    }); 
    mRequestQueue.add(request); 
} 


private ArrayList<QuestionData> parseJSONResponseQuestion(JSONArray response) { 
    if (!response.equals("")) { 
     ArrayList<QuestionData> questionDataArrayList = new ArrayList<>(); 
     try { 
      StringBuilder data = new StringBuilder(); 
      for (int i = 0; i < response.length(); i++) { 
       JSONObject currentQuestions = response.getJSONObject(i); 
       String text = currentQuestions.getString("text"); 
       String questionId = currentQuestions.getString("questionId"); 
       String votes = currentQuestions.getString("votes"); 
       System.out.println(votes+" VOTES"); 
       int voteInt=Integer.parseInt(votes); 

       System.out.println(voteInt); 
       String Answerid = currentQuestions.getString("id"); 
       String selectedId = currentQuestions.getString("selected"); 
       System.out.println(response.length() + "length"); 

       data.append(text + Answerid + "\n"); 

       System.out.println(data); 
       QuestionData questionData = new QuestionData(); 
       questionData.setMtext(text); 
       questionData.setVotes(votes); 
       questionData.setId(Answerid); 
       questionData.setSelected(selectedId); 
       mListblogs.add(questionData); 
      } 
      System.out.println(data.toString()); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return mListblogs; 
} 
+2

不要問你的項目輸出,請問是什麼錯誤以及如何糾正它。 – appukrb

+0

@appukrb並非每個問題都是錯誤,這是一個邏輯問題,我告訴了你到底是什麼 – eli

回答

0

「我從服務器獲取我的所有數據,所以我假設我需要做一個新的請求。」 - >實際上,您不需要再次從您的服務器重新請求數據。你只需要更新

private ArrayList<QuestionData> data =new ArrayList<>(); 
您AdapterQuestion

然後調用AdapterQuestion.notifyDataSetChanged();在獲得onUpVote或onDownVote的成功響應之後

「我是否需要創建一個新的適配器?以及JSON解析方法?我該如何做到這一點?!??!」 - >不需要

0

要在您的recyclerview中更改特定的ItemView,請使用以下代碼。

notifyItemChanged(index);

如果您想要更改onItemclick中的特定文本視圖,您必須獲取項目位置。

@Override 
public void onBindViewHolder(ViewQuestion holder, int position) { 
    holder.answerText.setText(currentObj.getMtext()); 
    holder.answerId.setText(currentObj.getId()); 
    holder.mVotes.setText(currentObj.getVotes()); 
    holder.mVotes.setTag(currentObj); 
    holder.mLikeButton.setTag(holder); 
} 

在你喜歡按鈕單擊事件更改代碼的形式大致如下

mLikeButton.setOnLikeListener(new OnLikeListener() { 
       @Override 
       public void liked(LikeButton likeButton) { 
        Voting vote = new Voting(); 
        vote.onUpVote(answerId()); 
final ViewQuestion holder=(ViewQuestionholder)likeButton.getTag(); 
currentObj co=(currentObj)holder.mVotes.getTag(); 
holder.mVotes.setText("16"); 
        System.out.print("Adapter Position"+getAdapterPosition()); 
       } 
       @Override 
       public void unLiked(LikeButton likeButton) { 
        Voting onDown=new Voting(); 
        onDown.onDownVote(answerId()); 
    final ViewQuestion holder=(ViewQuestionholder)likeButton.getTag(); 
currentObj co=(currentObj)holder.mVotes.getTag(); 
holder.mVotes.setText("14"); 
       } 
      }); 
+0

currentObj代表什麼?以及ViewQuestion擁有者是什麼? – eli

+0

你不能把currentObj拿出來嗎 – eli

+0

爲什麼我們不能拿?什麼是currentObj?這是一個Pojo類的權利。那我們可以。 – MathaN

相關問題