我有一个RecyclerView的代码,我添加了3个按钮,其中一个是Shortlist按钮。当我点击按钮时,成功来自服务器的凌空响应。当我点击按钮时我需要按钮的文字应该被改变而另一个的项目不会受到影响。
下面是我的CardAdapter类,我扩展了RecyclerView.Adapter
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private static final String url ="http://192.168.2.110/xp/shortlist1.php";
private static final String url1 = "http://192.168.2.110/xp/express_intrest.php";
private static final String KEY_MATRI_ID_TO="matriID_to";
private static final String KEY_MATRI_ID_BY="matriID_by";
SessionManager session;
public String matri_id_to, matri_id_by, str_gender;
String str;
//Imageloader to load image
private ImageLoader imageLoader;
private Context context;
//List to store all superheroes
List<SuperHero> superHeroes;
//Constructor of this class
public CardAdapter(List<SuperHero> superHeroes, Context context){
super();
//Getting all superheroes
this.superHeroes = superHeroes;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.superheroes_list, parent, false);
// Session class instance
session = new SessionManager(context);
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
matri_id_by = user.get(SessionManager.KEY_EMAIL);
str_gender = user.get(SessionManager.KEY_GENDER);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
//Getting the particular item from the list
final SuperHero superHero = superHeroes.get(position);
//Loading image from url
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(superHero.getImageUrl(), ImageLoader.getImageListener(holder.imageView, R.drawable.image, android.R.drawable.ic_dialog_alert));
holder.imageView.setImageUrl(superHero.getImageUrl(), imageLoader);
holder.textViewId.setText(superHero.getMglId());
holder.AgeHeight.setText(superHero.getAgeHeight()+" / "+superHero.getAge());
holder.Community.setText(superHero.getCommunity()+" / "+superHero.getCaste());
holder.Occupation.setText(superHero.getOccupation());
holder.Income.setText(superHero.getIncome());
// holder.btnShortlist.setText(superHero.getShortlist());
// holder.btnEI.setText(superHero.getExpress_Intrest());
holder.btnShortlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
superHero.setShortlist("Wait...");
holder.btnShortlist.setText(superHero.getShortlist());
matri_id_to = superHero.getMglId();
holder.shortlist(position);
}
});
str = superHero.getShortlist();
Log.e("sh____________",str);
if(str.toString().equalsIgnoreCase("shortlisted")) {
holder.btnShortlist.setText(str);
holder.btnShortlist.setBackgroundColor(Color.parseColor("#FF0E3671"));
holder.btnShortlist.setEnabled(false);
}
else{
holder.btnShortlist.setText(str);
}
holder.btnViewProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent n = new Intent(holder.itemView.getContext(),BlankActivity.class);
String str_id = holder.textViewId.getText().toString();
n.putExtra("ID",str_id);
holder.itemView.getContext().startActivity(n);
}
});
holder.btnEI.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public SuperHero getItem(int position){
return superHeroes.get(position);
}
@Override
public int getItemCount() {return superHeroes.size();}
class ViewHolder extends RecyclerView.ViewHolder{
public NetworkImageView imageView;
public TextView textViewId;
public TextView AgeHeight;
public TextView Community;
public TextView Occupation;
public TextView Income;
public Button btnShortlist;
public Button btnViewProfile;
public Button btnEI;
//Initializing Views
public ViewHolder(final View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewId = (TextView) itemView.findViewById(R.id.textViewId);
AgeHeight = (TextView) itemView.findViewById(R.id.AgeHeight);
Community = (TextView) itemView.findViewById(R.id.Community);
Occupation = (TextView) itemView.findViewById(R.id.Occupation);
Income = (TextView) itemView.findViewById(R.id.Income);
btnShortlist = (Button) itemView.findViewById(R.id.btnshort);
btnViewProfile = (Button) itemView.findViewById(R.id.buttonViewProfile);
btnEI = (Button) itemView.findViewById(R.id.btnExpressIntrest);
}
public void shortlist(final int position) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equalsIgnoreCase("success")) {
superHeroes.get(position).setShortlist("Shortlisted");
holder.btnShortlist.setText(superHero.getShortlist());
notifyDataSetChanged();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_MATRI_ID_BY, matri_id_by);
params.put(KEY_MATRI_ID_TO, matri_id_to);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}
}
}
答案 0 :(得分:1)
问题在于onClickListener。更新如下所述的代码:
holder.btnShortlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
superHero.setShortlist("Wait...");
holder.btnShortlist.setText(superHero.getShortlist());
matri_id_to = superHero.getMglId();
holder.shortlist(position);
}
});
如果您不这样做,那么当您滚动时您的视图将被重置,或者该项目将根据您添加的条件进行回收:
if(str.toString().equalsIgnoreCase("shortlisted")) {
holder.btnShortlist.setText(str);
holder.btnShortlist.setBackgroundColor(Color.parseColor("#FF0E3671"));
holder.btnShortlist.setEnabled(false);
}
else{
holder.btnShortlist.setText(str);
}
此外,
public void shortlist(final ViewHolder holder, final int position) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equalsIgnoreCase("success")) {
// If response is success then button's text should be changed i.e "Shortlisted"
superHeroes.get(position).setShortlist("Shortlisted");
holder.btnShortlist.setText(superHero.getShortlist());
notifyDataSetChanged();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_MATRI_ID_BY, matri_id_by);
params.put(KEY_MATRI_ID_TO, matri_id_to);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}
如果它仍然不会出现,则在onClick()的最后调用notifyDataSetChanged()。
更新您的条件并获得所需的结果:
如果在if块中更改适配器中的任何UI,则应该在UI中正确反映UI的相同情况,否则当视图刷新时,UI可能无法按照需要进行回收。因此,根据您提到的条件更新了代码:
if (str.toString().equalsIgnoreCase("shortlisted")) {
holder.btnShortlist.setText(str);
holder.btnShortlist.setBackgroundColor(Color.parseColor("#FF0E3671"));
holder.btnShortlist.setEnabled(false);
} else {
holder.btnShortlist.setText(str);
//Color you want in else case
holder.btnShortlist.setBackgroundColor(Color.parseColor("#FF0E3671"));
//State you want in else case
holder.btnShortlist.setEnabled(false);
}
答案 1 :(得分:0)
这样做的简单方法是在您想要更改的视图上传递引用。
holder.btnShortlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.btnShortlist.setText("Wait...");
matri_id_to = superHero.getMglId();
shortlist( holder.btnShortlist,position); // change this line
}
});
然后在你的回复中你可以做到这一点。
public void shortlist(final Button button , final int position) { // Pass button as param
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equalsIgnoreCase("success")) {
// If response is success then button's text should be changed i.e "Shortlisted"
button.setText("ShortListed"); // finally set your text
notifyDataSetChanged();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put(KEY_MATRI_ID_BY,matri_id_by);
params.put(KEY_MATRI_ID_TO,matri_id_to);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
}
修改强>
您必须在onClickListener
中移动ViewHolder
。
public ViewHolder(final View itemView) {
super(itemView);
imageView = (NetworkImageView) itemView.findViewById(R.id.imageViewHero);
textViewId = (TextView) itemView.findViewById(R.id.textViewId);
AgeHeight = (TextView) itemView.findViewById(R.id.AgeHeight);
Community = (TextView) itemView.findViewById(R.id.Community);
Occupation = (TextView) itemView.findViewById(R.id.Occupation);
Income = (TextView) itemView.findViewById(R.id.Income);
btnShortlist = (Button) itemView.findViewById(R.id.btnshort);
btnShortlist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnShortlist.setText("Wait...");
matri_id_to = superHeroes.get(getAdapterPosition()).getMglId();
shortlist(btnShortlist,position); // change this line
}
});
}