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请求和一个解析方法,如下所示,我只想更新&#34;投票&# 34;当按下按钮时我该怎么办?
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;
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
我假设您为LinearLayoutManager
使用RecyclerView
。
将点击的位置从ViewQuestion's
onClick()
传递到onUpVote()
和onDownVote()
方法。
从onUpVote()
和onDownVote()
方法发布Broadcast
,指定intent.putExtras
中的任何数据,并包含点击的位置。
现在在Activity
上,写一个BroadcastReceiver
,其中包含broadcasts
或onUpVote()
方法发送的onDownVote()
。现在从Activity
打电话给你
LinearLayoutManager.findViewByPosition(position)
方法获取对您点击的View
的引用。现在,您可以使用findViewById()
方法查找所需的view
并更新值。
答案 2 :(得分:0)
听起来您的应用程序需要一些架构工作。您是否考虑过仅在需要将数据读取或写入文件或网络或类似内容时限制使用JSON。如果你在模型中混合使用JSON,那么你可能会编写比你需要的代码更多的代码。
只是一个想法...