【问题标题】:How can I choose to update a particular view with a JSON request如何选择使用 JSON 请求更新特定视图
【发布时间】:2016-03-31 05:39:13
【问题描述】:
    public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{
    private LayoutInflater mLayoutInflater;
    private ArrayList<QuestionData> mListblogs =new ArrayList<>();
    public AdapterQuestion(Context context){

        mLayoutInflater=LayoutInflater.from(context);

    }
    public void setBloglist(ArrayList<QuestionData> listBlogs){
        this.mListblogs =listBlogs;
        notifyItemRangeChanged(0, listBlogs.size());
    }

    @Override
    public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= mLayoutInflater.inflate(R.layout.customquestion, null);
        ViewQuestion holder=new ViewQuestion(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewQuestion holder, int position) {
        QuestionData currentBlog= mListblogs.get(position);
        holder.answerText.setText(currentBlog.getMtext());
        holder.answerId.setText(currentBlog.getId());
        holder.mVotes.setText(currentBlog.getVotes());
    }

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

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

        public ViewQuestion (View view){
            super(view);
            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(convertToString(),mVotes);
                }
                @Override
                public void unLiked(LikeButton likeButton) {
                    Voting onDown=new Voting();
                    onDown.onDownVote(convertToString());
                }
            });

        }
        public String getVoteView(){
            String voteView=mVotes.getText().toString();
            return voteView;
        }
        public String convertToString(){
            String converted=answerId.getText().toString();
            return converted;
        }
    }
}

    public class Voting {

    public void onUpVote(String Id,final TextView VoteView) {
        final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
        final String PUT_VOTE_UP = "url";
        StringRequest PostVoteUp = new StringRequest(Request.Method.PUT, PUT_VOTE_UP, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                VoteView.setText("likes");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        mrequestQueue.add(PostVoteUp);
        System.out.println("VOTED UP");
    }
    public void onDownVote( String Id) {
        final RequestQueue mrequestQueue = VolleySingleton.getInstance().getRequestQueue();
        final String PUT_VOTE_DOWN = "url";
        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);
        System.out.println("VOTED DOWN");
    }

}

我有两个类,一个用于片段活动的适配器类和一个投票类。当用户按下按钮时,我在我的适配器类中调用投票类,但是,我的片段活动中还有一个 JSON 请求和一个解析方法,如下所示,我只想在按钮时更新“投票”按下了怎么办?

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 votes = currentQuestions.getString("votes");
                int voteInt=Integer.parseInt(votes);
                QuestionData questionData = new QuestionData();
                questionData.setMtext(text);
                questionData.setVotes(votes);
            }
            System.out.println(data.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return mListblogs;
}

【问题讨论】:

  • 发布更多细节
  • 您是在询问使用 json 更新 listView 数据吗?很难理解你想要达到的目标
  • 它是一个回收站视图
  • 正如你所说的那样扩展 RecyclerView.Adapter
  • 这无关紧要,只是想了解您的问题。

标签: android json parsing android-fragments


【解决方案1】:

首先您应该在适配器中添加一个方法来更新单个 QuestionData。例如:

public void setBlogItem(int index, QuestionData questionData) {
    if (mListblogs.get(index).getVotes() != questionData.getVotes()) {
        mListblogs.set(index, questionData);
        notifyItemChanged(index);
    }
}

第二。更新问题。为此,您必须知道该项目的索引。我建议整个列表不要改变。所以,修改你的 json 解析器:

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 votes = currentQuestions.getString("votes");
                int voteInt=Integer.parseInt(votes);
                QuestionData questionData = new QuestionData();
                questionData.setMtext(text);
                questionData.setVotes(votes);
                recyclerAdapter.setBlogItem(i, questionData); /// Put attention here!
            }
            System.out.println(data.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return mListblogs;
}

【讨论】:

    【解决方案2】:

    我假设您使用 LinearLayoutManager 作为您的 RecyclerView

    将点击位置从您的ViewQuestion's onClick() 传递给您的onUpVote()onDownVote() 方法。

    通过onUpVote()onDownVote() 方法,发布Broadcast,指定intent.putExtras 中的任何数据,并包括点击位置。

    现在在您的Activity 上,编写一个BroadcastReceiver,它捕获从onUpVote()onDownVote() 方法发送的broadcasts。现在从Activity,致电您的 LinearLayoutManager.findViewByPosition(position) 方法来获取对您被点击的View 的引用。现在您可以使用findViewById() 方法找到您需要的view 并更新值。

    【讨论】:

    • 老兄,这绝不是一种有效的方法
    • 嗨,伊莱。嗯,它并没有你想象的那么低效。我想你明白,否则很难与活动沟通!这是我唯一想到的!大声笑!
    【解决方案3】:

    听起来您的应用程序需要一些架构工作。您是否考虑过仅在需要将数据读取或写入文件或网络或类似的东西时才使用 JSON。如果您在模型中混合了 JSON,那么您可能会编写比您需要的更多的代码。

    只是一个想法......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多