我正在使用RecyclerView custum适配器,其中数据是从getter和setter方法获取的,并且superHero.getStatus()存在一个问题,即如果该位置上有“已删除”,则所有视图可见性将为View.GONE否则所有观点都将显示在该项目上。有时它不能像下面的代码条件那样工作。另一个问题是当我点击按钮然后它对另一个视图的影响。请检查以下代码。并告诉我应该怎样做才能改进它。我应该使用getTag()和setTag()方法来解决这个问题吗?
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private static final String url1 = "************************.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_status,str_EI;
//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_shortlist_me, 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);
holder.textViewId.setText(superHero.getMglId());
str_status = superHero.getStatus();
if(str_status.equalsIgnoreCase("deleted"))
{
holder.tvCommunity.setText("This profile has been deleted");
holder.tvCommunity.setTextColor(Color.RED);
holder.Community.setVisibility(View.GONE);
holder.AgeHeight.setVisibility(View.GONE);
holder.Occupation.setVisibility(View.GONE);
holder.Income.setVisibility(View.GONE);
holder.shortlist_on.setVisibility(View.GONE);
holder.imageView.setVisibility(View.GONE);
holder.tvAgeHeight.setVisibility(View.GONE);
holder.tvIncome.setVisibility(View.GONE);
holder.tvShortlistOn.setVisibility(View.GONE);
holder.tvOccupation.setVisibility(View.GONE);
holder.btnEI.setVisibility(View.GONE);
holder.btnViewProfile.setVisibility(View.GONE);
}
else
{
holder.AgeHeight.setText(superHero.getAge()+" / "+superHero.getAgeHeight());
holder.Community.setText(superHero.getCommunity()+" / "+superHero.getCaste());
holder.Occupation.setText(superHero.getOccupation());
holder.Income.setText(superHero.getIncome());
holder.shortlist_on.setText(superHero.getShortlisted_on());
holder.btnEI.setText(superHero.getExpress_Intrest());
//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);
}
str_EI = superHero.getExpress_Intrest();
if(str_EI.toString().equalsIgnoreCase("Accepted")) {
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else if(str_EI.toString().equalsIgnoreCase("Reject")){
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else if(str_EI.toString().equalsIgnoreCase("Declined")){
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else if(str_EI.toString().equalsIgnoreCase("Pending..")){
holder.btnEI.setText(str_EI);
holder.btnEI.setBackgroundColor(Color.parseColor("#FF045B49"));
holder.btnEI.setEnabled(false);
}
else
{
holder.btnEI.setText(str_EI);
}
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) {
superHero.setExpress_Intrest("Wait...");
holder.btnEI.setText(superHero.getExpress_Intrest());
matri_id_to = superHero.getMglId();
holder.expressInterest(position);
}
});
}
@Override
public int getItemCount() {
return superHeroes.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
//Views
public NetworkImageView imageView;
public TextView textViewId;
public TextView AgeHeight,tvAgeHeight;
public TextView Community, tvCommunity;
public TextView Occupation, tvOccupation;
public TextView Income, tvIncome;
public TextView shortlist_on, tvShortlistOn;
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);
tvAgeHeight = (TextView) itemView.findViewById(R.id.tvAgeHeight);
AgeHeight = (TextView) itemView.findViewById(R.id.AgeHeight);
tvCommunity = (TextView) itemView.findViewById(R.id.tvCommunity);
Community = (TextView) itemView.findViewById(R.id.Community);
tvOccupation = (TextView) itemView.findViewById(R.id.tvOccupation);
Occupation = (TextView) itemView.findViewById(R.id.Occupation);
tvIncome = (TextView) itemView.findViewById(R.id.tvIncome);
Income = (TextView) itemView.findViewById(R.id.Income);
tvShortlistOn = (TextView) itemView.findViewById(R.id.tvShortlistOn);
shortlist_on = (TextView) itemView.findViewById(R.id.shortlistOn);
btnViewProfile = (Button) itemView.findViewById(R.id.viewProfile);
btnEI = (Button) itemView.findViewById(R.id.sendEI);
}
public void expressInterest(final int position) {
StringRequest stringRequest1 = new StringRequest(Request.Method.POST, url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equalsIgnoreCase("success")) {
superHeroes.get(position).setExpress_Intrest("Pending..");
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(stringRequest1);
}
}
}
答案 0 :(得分:1)
1)首先发布可见视图
您需要在else部分中放置其他视图的可见性代码,因为如果状态从已删除状态更改为其他状态,则您的视图将不可见,因此您需要输入可见代码。
离。我在你的其他部分添加了四行。请检查。并根据您的要求添加
if(str_status.equalsIgnoreCase("deleted"))
{
holder.tvCommunity.setText("This profile has been deleted");
holder.tvCommunity.setTextColor(Color.RED);
holder.Community.setVisibility(View.GONE);
holder.AgeHeight.setVisibility(View.GONE);
holder.Occupation.setVisibility(View.GONE);
holder.Income.setVisibility(View.GONE);
holder.shortlist_on.setVisibility(View.GONE);
holder.imageView.setVisibility(View.GONE);
holder.tvAgeHeight.setVisibility(View.GONE);
holder.tvIncome.setVisibility(View.GONE);
holder.tvShortlistOn.setVisibility(View.GONE);
holder.tvOccupation.setVisibility(View.GONE);
holder.btnEI.setVisibility(View.GONE);
holder.btnViewProfile.setVisibility(View.GONE);
}
else
{
//Add this
holder.Community.setVisibility(View.VISIBLE);
holder.AgeHeight.setVisibility(View.VISIBLE);
holder.Occupation.setVisibility(View.VISIBLE);
holder.Income.setVisibility(View.VISIBLE);
holder.AgeHeight.setText(superHero.getAge()+" / "+superHero.getAgeHeight());
holder.Community.setText(superHero.getCommunity()+" / "+superHero.getCaste());
holder.Occupation.setText(superHero.getOccupation());
holder.Income.setText(superHero.getIncome());
holder.shortlist_on.setText(superHero.getShortlisted_on());
holder.btnEI.setText(superHero.getExpress_Intrest());
//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);
}