2017-09-05 41 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

是的,那就是我在說的。代碼也存在一些問題。 –