2017-09-05 43 views
0

因此,我在CommentsAdapter类中使用了此getView方法,它基本上允许用户在显示upvote计数的同时在评论部分中投票或下投注释。我可以通过相应的用户界面更改(蓝色按钮用于未投票和橙色用于投票)来上调(并取消投票)。如何在Android中刷新后保持UI更改

但是,一旦我刷新了屏幕,无论它是否被upvoted,该按钮将总是恢复为蓝色,并且投票计数返回原始计数。我没有检查数据库,记录了正确的upvote计数和操作。有小费吗?

public class CommentsAdapter extends ArrayAdapter<Post> { 
    private int layout; 
    private Context context; 
    private ArrayList<Post> postList = new ArrayList<>(); 

    public CommentAdapter(@NonNull Context cont, @LayoutRes int textViewResourceId, @NonNull ArrayList<Post> objects) { 
    super(cont, textViewResourceId, objects); 
    layout = textViewResourceId; 
    context = cont; 
    postList = objects; 
    } 

    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
     final ViewHolder v; 
     final Post comment = postList.get(position); 
     final String mimeType = "text/html"; 
     final String encoding = "UTF-8"; 
     final SharedPreferences prefs = context.getSharedPreferences("user_session", MODE_PRIVATE); 
     final String sessionKey = prefs.getString("session_key", ""); 
     String htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"comments.css\" />" + comment.content; 

     if (convertView == null) { 
      v = new ViewHolder(); 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(layout, parent, false); 

      v.upvotedIcon = convertView.findViewById(R.id.navbar_upvoted_icon); 
      v.upvoteIcon = convertView.findViewById(R.id.navbar_upvote_icon); 
      v.upvotedText = convertView.findViewById(R.id.navbar_upvoted_text); 
      v.upvoteText = convertView.findViewById(R.id.navbar_upvote_text); 

      v.upvoteButton = convertView.findViewById(R.id.navbar_upvote_button); 
      v.upvotedButton = convertView.findViewById(R.id.navbar_upvoted_button); 

      v.upvotedIcon.setTypeface(fontAwesome); 
      v.upvoteIcon.setTypeface(fontAwesome); 
      v.upvotedText.setTypeface(opensans); 
      v.upvoteText.setTypeface(opensans); 

      convertView.setTag(v); 
     } else { 
      v = (ViewHolder) convertView.getTag(); 
     } 

     v.upvoteText.setText(String.format(Locale.ENGLISH, "%d", comment.stats.upvotes)); 
     v.upvotedText.setText(String.format(Locale.ENGLISH, "%d", comment.stats.upvotes + 1)); 

     if (comment.hasReacted) { 
      v.upvoteButton.setVisibility(View.GONE); 
      v.upvotedButton.setVisibility(View.VISIBLE); 
     } else { 
      v.upvotedButton.setVisibility(View.GONE); 
      v.upvoteButton.setVisibility(View.VISIBLE); 
     } 

     v.upvoteButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       setButtonState(true, comment, v); 
       Call<JsonObject> call = MyApi.endpoint().upVotePost(sessionKey, comment.id); 
       call.enqueue(new Callback<JsonObject>() { 
        @Override 
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { 
         if(response.code() != 200) { 
          // show upvote icon 
          setButtonState(false, comment, v); 
          Toast.makeText(context, "Cannot upvote for the moment, try again later.", Toast.LENGTH_SHORT).show(); 
         } 
        } 

        @Override 
        public void onFailure(Call<JsonObject> call, Throwable t) { 
         // show upvote icon 
         setButtonState(false, comment, v); 
         Toast.makeText(context, "Cannot connect to the server, try again later.", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 


     }); 

     v.upvotedButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // show upvote icon 
       setButtonState(false, comment, v); 
       Call<JsonObject> call = MyApi.endpoint().downVotePost(sessionKey, comment.id); 
       call.enqueue(new Callback<JsonObject>() { 
        @Override 
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { 
         if(response.code() != 200) { 
          // show upvoted icon 
          setButtonState(true, comment, v); 
          Toast.makeText(context, "Cannot undo your upvote for the moment, try again later.", Toast.LENGTH_SHORT).show(); 
         } 
        } 

        @Override 
        public void onFailure(Call<JsonObject> call, Throwable t) { 
         // show upvoted icon 
         setButtonState(true, comment, v); 
         Toast.makeText(context, "Cannot connect to the server, try again later.", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
     }); 

     return convertView; 
    } 
} 
+1

你可以在你的代码中使用'notifyDataSetChanged();'吗? – KeLiuyue

+0

@KeLiuyue我该怎么做这个课程? 'CommentsAdapter'扩展'ArrayAdapter'顺便说一句。 – Bargain23

+1

您可以添加代码吗,您如何使用'postList'初始化适配器? –

回答

2

您应该添加一个属性到你的模型是一个状态,包含“upvoted”,“downvoted”等,或{空串}值。然后,当你的数据被采取行动相应地设置。然后,当数据重新加载时,请检查模型的状态,并使用每个状态的正确图标进行显示。 (我只是使用字符串作为状态值,但你可以做更优雅的事情)。

编辑:

正如一些用户指出,确保你正在修改你的数据库值后调用notifyDataSetChanged。另外,如果您要从远程服务器获取数据,请确保在重新载入之前已更新upvote/downvote值(或至少将远程数据正确合并到本地数据中)。

+1

这是回答这个问题吗?我不太确定。 –

+1

他的问题不是代码问题,而是数据架构问题。我可以添加代码以使视图根据我提到的状态显示,但这相当简单。问题是Bargain23没有保存除UI之外的按钮状态记录,所以当UI触发重绘时,他会失去操作。 – Jlange

+1

是的,那就是我在说的。代码也存在一些问题。 –