我有一个视图,在该视图中我有一个textview我想更新textview?

时间:2016-04-06 05:38:50

标签: java android json

我有三个类,我的适配器类,Voting类和一个活动,我的JSONrequest如下所示。问题是你可以看到我在我的活动中调用我的JSONrequest,它将获得一个视图列表,里面那个视图我有一个mVote textview。在我的适配器中你也可以看到我有一个likeButton,当有人按下我希望mVotes从0更改为1.我从服务器获取所有数据所以我假设我需要提出新请求,我需要吗制作一个新的适配器?和JSON解析方法?我该怎么做呢?!??!

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 个答案:

答案 0 :(得分:0)

&#34;我从服务器获取所有数据,因此我假设我需要提出新请求。&#34; - &GT;实际上,您不需要再次从服务器重新请求数据。你只需要更新

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

然后调用AdapterQuestion.notifyDataSetChanged();从onUpVote或onDownVote获得成功回复后

&#34;我需要制作新的适配器吗?和JSON解析方法?我该怎么做?!??!&#34; - &GT;不需要

答案 1 :(得分:0)

要更改Recyclerview中的特定ItemView,请使用以下代码。

notifyItemChanged(索引);

如果您想更改onItemclick中的特定Textview,您必须获取项目位置。

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