我有一个让我疯狂的小问题 所以我试图创建一个小型的社交网络应用程序"在Android上。
在我的主片段中,我有一个包含帖子的RecyclerView(它就像新闻Feed一样) 在每个项目中都有一个像按钮和一个注释按钮,在onBindViewHolder函数的回收器viewAdapter中,我使用一个Volley Request来获取用户喜欢的所有帖子,每次我抓住一个我设置了Like的文本颜色textView(J' aime in French)蓝色,如下图所示。
如果我点击另一个Like按钮它会变成蓝色等添加到数据库中,但问题是当我想要删除它时再次点击它不能在回收器适配器i&#中工作39;如果textview为蓝色,则使用IF条件进行检查,如果它没有设置默认颜色,我从数据库中删除了类似物,这里是适配器的代码。
package com.example.salim.projet.model;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.example.salim.projet.R;
import com.example.salim.projet.fragments.HomeFragment;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Created by salim on 24/04/18.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
int clickedPosition = -1;
String send_url = "http://10.0.3.2/sendlike.php";
String drop_url = "http://10.0.3.2/droplike.php";
String email;
ArrayList<Likes> arrayLikes = new ArrayList<>();
ArrayList<Publication> arrayList = new ArrayList<>();
public RecyclerAdapter(ArrayList<Publication> arrayList, String email)
{
this.arrayList = arrayList;
this.email = email;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_row, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
Context context;
String likes_url = "http://10.0.3.2/likes.php?email="+email;
holder.Nom.setText(arrayList.get(position).getNom());
holder.Prenom.setText(arrayList.get(position).getPrenom());
holder.Contenu.setText(arrayList.get(position).getContenu());
holder.Date.setText(arrayList.get(position).getDate());
holder.Like.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickedPosition = position;
notifyDataSetChanged();
}
});
// RECUPERATION LIKES
// HERE IS WHERE I CATCH ALL THE POSTS THAT THE USER LIKED FROM DATABASE ON A JSON
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, likes_url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
int count = 0;
while (count < response.length())
{
try {
JSONObject jsonObject = response.getJSONObject(count);
Likes like = new Likes(jsonObject.getInt("ID"), jsonObject.getInt("LikeUser"), jsonObject.getInt("PostLiked"));
arrayLikes.add(like);
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
if (!arrayLikes.isEmpty())
{
int i;
for (i = 0; i < arrayLikes.size(); i++)
{
if (arrayLikes.get(i).getPost_id() == arrayList.get(position).getId()) {
holder.Like.setTextColor(ContextCompat.getColor(holder.Like.getContext(), R.color.startblue));
}
}
}
// Log.i("DEBUG", " Position item : " + position + " Position du I : " + i + " ArrayLike Post ID : "+ arrayLikes.get(position).getPost_id() + " == Publication ID : " + arrayList.get(position).getId());
// if (arrayLikes.get(position).getPost_id() == arrayList.get(position).getId()) {
// holder.Like.setTextColor(ContextCompat.getColor(holder.Like.getContext(), R.color.startblue));
// }
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
Mysingleton.getInstance(holder.Like.getContext()).addToRequestQueue(jsonArrayRequest);
// FIN RECUP LISTE LIKES
// HERE IS WHERE I HANDLE THE CLICKS ON THE LIKE BUTTON
if(clickedPosition == position) {
if (holder.Like.getCurrentTextColor() == (ContextCompat.getColor(holder.Like.getContext(), R.color.startblue)))
{
holder.Like.setTextColor(ContextCompat.getColor(holder.Like.getContext(), R.color.defaultt));
StringRequest stringRequest = new StringRequest(Request.Method.POST, drop_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String,String>();
params.put("email", email);
params.put("postid", String.valueOf(arrayList.get(position).getId()));
return params;
}
};
Mysingleton.getInstance(holder.Like.getContext()).addToRequestQueue(stringRequest);
}
else
{
holder.Like.setTextColor(ContextCompat.getColor(holder.Like.getContext(), R.color.startblue));
StringRequest stringRequest = new StringRequest(Request.Method.POST, send_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String,String>();
params.put("email", email);
params.put("postid", String.valueOf(arrayList.get(position).getId()));
return params;
}
};
Mysingleton.getInstance(holder.Like.getContext()).addToRequestQueue(stringRequest);
}
}
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder
{
TextView Nom, Prenom, Contenu, Date, Like;
public MyViewHolder(View itemView) {
super(itemView);
Nom = (TextView) itemView.findViewById(R.id.name);
Prenom = (TextView) itemView.findViewById(R.id.surName);
Contenu = (TextView) itemView.findViewById(R.id.feedText);
Date = (TextView) itemView.findViewById(R.id.date);
Like = (TextView) itemView.findViewById(R.id.likeView);
}
}
}
所有的东西都发生在OnBindViewHolder上,希望我的问题很明确,如果我可以添加任何其他东西,只需告诉我,谢谢! 也许这个问题对你来说可能听起来很弱,但我不是专业人士,而且每次我遇到另一个问题时,我都会通过2天以上的时间来解决这个问题。