Java 我有一个视图,在该视图中我有一个文本视图我想更新该文本视图吗?
我有三个类,适配器类、投票类和一个活动,其中我的JSONrequest如下所示。问题是,你可以看到我在活动中调用我的JSONrequest,它将获得一个视图列表,在该视图中我有一个mVote textview。在我的适配器中,您还可以看到,当有人按下时,我有一个like按钮,希望mVotes从0更改为1。我从服务器获取所有数据,因此我假设我需要发出一个新的请求,是否需要向服务器发出一个新的适配器?JSON解析方法呢?我该怎么做Java 我有一个视图,在该视图中我有一个文本视图我想更新该文本视图吗?,java,android,json,Java,Android,Json,我有三个类,适配器类、投票类和一个活动,其中我的JSONrequest如下所示。问题是,你可以看到我在活动中调用我的JSONrequest,它将获得一个视图列表,在该视图中我有一个mVote textview。在我的适配器中,您还可以看到,当有人按下时,我有一个like按钮,希望mVotes从0更改为1。我从服务器获取所有数据,因此我假设我需要发出一个新的请求,是否需要向服务器发出一个新的适配器?JSON解析方法呢?我该怎么做 public class AdapterQuestion exten
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;
}
}
}
公共类适配器问题扩展了RecyclerView.Adapter{
私人停车场平面布置;
private ArrayList data=new ArrayList();
公共适配器问题(上下文){
//从上下文中获取
mLayoutInflater=LayoutInflater.from(上下文);
}
公共日志列表(ArrayList数据){
这个数据=数据;
notifyItemRangeChanged(0,this.data.size());
}
@凌驾
public ViewQuestion onCreateViewHolder(视图组父级,int-viewType){
ViewQuestion holder=新的ViewQuestion(视图);
报税表持有人;
}
@凌驾
public void onBindViewHolder(ViewQuestion holder,int位置){
holder.answerText.setText(currentObj.getMtext());
holder.answerId.setText(currentObj.getId());
holder.mVotes.setText(currentObj.getVotes());
holder.mLikeButton.setTag(currentObj);
}
@凌驾
public int getItemCount(){
返回data.size();
}
公共类ViewQuestion扩展了RecyclerView.ViewHolder{
私有文本查看应答文本;
私有文本视图应答器ID;
私有文本视图mVotes;
私密的像按钮一样的按钮;
公共视图问题(视图项视图){
超级(项目视图);
answerText=(TextView)itemView.findViewById(R.id.answerText);
answerId=(TextView)itemView.findViewById(R.id.answerId);
mVotes=(TextView)itemView.findViewById(R.id.VoteTextView);
mLikeButton=(LikeButton)itemviewbyd(R.id.heart_按钮);
setOnLikeListener(新的OnLikeListener(){
@凌驾
公共按钮(LikeButton LikeButton){
投票表决=新的投票表决();
投票.onUpVote(answerId());
系统输出打印(“适配器位置”+getAdapterPosition());
}
@凌驾
公共无效未链接(LikeButton LikeButton){
在镇上投票=新投票();
onDown.onDownVote(answerId());
}
});
}
公共字符串getVoteView(){
字符串voteView=mVotes.getText().toString();
返回voteView;
}
公共字符串应答器ID(){
转换的字符串=answerId.getText().toString();
转换收益;
}
公共整数转换(){
转换的字符串=answerId.getText().toString();
int ConvertedInt=Integer.parseInt(已转换);
返回转换;
}
}
}
投票表决
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);
}
公共类投票{
私人排球单打;
私有请求队列mRequestQueue;
私人AdapterNoteUpdate MadapterVoces;
private ArrayList updateVotes=new ArrayList();
公共void onUpVote(最终字符串应答ID){
final RequestQueue MRRequestQueue=VolleySingleton.getInstance().getRequestQueue();
最后一个字符串PUT\u VOTE\u UP=“url”+answerId+“url\n”;
StringRequest PostVoteUp=新建StringRequest(Request.Method.PUT,PUT\u VOTE\u UP,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
mrequestQueue.add(PostVoteUp);
}
公开无效onDownVote(最终字符串应答ID){
System.out.println(“投票否决”);
final RequestQueue MRRequestQueue=VolleySingleton.getInstance().getRequestQueue();
最后一个字符串PUT_VOTE_DOWN=“url”+answerId+“urul”;
StringRequest PostVoteUp=新的StringRequest(Request.Method.PUT,PUT\u VOTE\u DOWN,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
System.out.println(“************回答”+错误+“错误”);
}
});
mrequestQueue.add(PostVoteUp);
}
JSON请求类
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;
}
public void JsonRequestMethod(字符串Id){
mVolleySingleton=VolleySingleton.getInstance();
mRequestQueue=mVolleySingleton.getRequestQueue();
最终字符串URL\u ANSWER=“URL”+Id+“URL”;
JsonArrayRequest=newjsonarrayrequest(request.Method.GET,URL\u-ANSWER,(String)null,new-Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
mListblogs.clear();
mListblogs=parseJSONResponseQuestion(响应);
mAdapterQuestion.setBloglist(mListblogs);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
系统输出打印项次(错误);
}
});
mRequestQueue.add(请求);
}
专用ArrayList parseJSONResponseQuestion(JSONArray响应){
如果(!response.equals(“”){
ArrayList questionDataArrayList=新的ArrayList();
试一试{
StringBuilder数据=新的StringBuilder();
对于(int i=0;iprivate ArrayList<QuestionData> data =new ArrayList<>();
@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");
}
});