У меня есть один ключ со строкой со значениями, разделенными запятыми.то есть "template_id": "9,10" / "template_id": "0".Из API я получаю 10 разных параметров для каждого идентификатора.
template_id=0 => Blank
template_id=1 => Out of Stock
template_id=2 => Birthday
template_id=3 => Gold Exclusive
template_id=4 => Selling Fast
template_id=5 => Almost out of Stock
template_id=6 => Use Parameter name "Points"
template_id=7 => Use Parameter name "Price"
template_id=8 => Use Parameter name "Venue"
template_id=9 => Use Parameter name "No of Locations"
Я использую Recyclerview
.Ниже я добавил код для проверки идентификаторов и добавил текстовое представление settext в соответствии с идентификаторами:
if (model.getTemplate_id().contains("10")) {
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
}
if (model.getTemplate_id().contains("9")) {
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
holder.mBinding.tvMembership.setText(model.getNo_of_locations() + " Locations");
holder.mBinding.tvPrice.setText(model.getStart_to_end());
}
if (model.getTemplate_id().contains("8")) {
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
holder.mBinding.tvMembership.setText(model.getVenue());
holder.mBinding.tvPrice.setText(model.getStart_to_end());
}
if (model.getTemplate_id().contains("7")) {
holder.mBinding.ivPrice.setImageResource(R.drawable.ic_reward_dollar);
holder.mBinding.tvPrice.setText(model.getPrice());
holder.mBinding.tvPrice.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.GONE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("6")) {
holder.mBinding.ivPrice.setImageResource(R.drawable.ic_reward_point);
holder.mBinding.tvPrice.setText(model.getPoints());
holder.mBinding.tvPrice.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.GONE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("5")) {
holder.mBinding.tvMembership.setText(R.string.almost_out_of_stock);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_selling_fast);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("4")) {
holder.mBinding.tvMembership.setText(R.string.selling_fast);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_selling_fast);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("3")) {
holder.mBinding.tvMembership.setText(R.string.gold_exclusive);
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_gold_exclusive);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.colorGold));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("2")) {
holder.mBinding.tvPrice.setText(mCMSTextData.getBirthday());
holder.mBinding.ivPrice.setImageResource(R.drawable.ic_birthday);
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("1")) {
holder.mBinding.tvMembership.setText(R.string.out_of_stock);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_out_of_stock);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.colorAccent));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("0")) {
holder.mBinding.llMembership.setVisibility(View.GONE);
holder.mBinding.llPrice.setVisibility(View.GONE);
}
Но каждый раз, когда это текстовое представление переопределялось другими значениями.Я пытался взять 20 TextView
и условия, но все еще не работает.
============ РЕДАКТИРОВАТЬ ==============
Класс адаптера:
public class CatalogueAdapter extends RecyclerView.Adapter<CatalogueAdapter.ViewHolder> {
Context context;
private List<CatalogueData> dataList;
private Activity activity;
private ImageLoader imageLoader;
private DisplayImageOptions defaultOptions;
private HashMap<String, Integer> hashMap = new HashMap<>();
private OnRecyclerItemClickListener mListener;
private CMSTextData mCMSTextData = new CMSTextData();
private CustomSharedPreference prefManager;
public CatalogueAdapter(Context context, List<CatalogueData> dataList, OnRecyclerItemClickListener mListener) {
this.context = context;
this.dataList = dataList;
this.mListener = mListener;
imageLoader = ImageLoader.getInstance();
defaultOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(R.color.colorTextHint)
.showImageForEmptyUri(R.drawable.ic_mall_logo)
.showImageOnFail(R.drawable.ic_mall_logo)
.cacheOnDisk(true)
.cacheInMemory(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();
prefManager = PrefManager.getInstance(context).getPrefs();
mCMSTextData = new Gson().fromJson(prefManager.getString(PARAMS.KEY_CMS_TEXT, ""), CMSTextData.class);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RowCatalogueBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
, R.layout.row_catalogue, parent, false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
if (dataList.size() > 0) {
final CatalogueData model = dataList.get(position);
holder.mBinding.setCurrentData(model);
imageLoader.displayImage(model.getImage4x3(), holder.mBinding.ivCatalogue, defaultOptions);
imageLoader.displayImage(model.getLevel_logo_image(), holder.mBinding.ivLogo, defaultOptions);
/* template_id=0 => Blank
template_id=1 => Out of Stock
template_id=2 => Birthday
template_id = 3 =>Gold Exclusive
template_id = 4 =>Selling Fast
template_id = 5 =>Almost out of Stock
template_id = 6 =>Points
template_id = 7 =>Price
template_id = 8 =>Venue(Event hall A, AMK Hub)
template_id = 9 =>No of Locations(3 Locations) */
String n1[] = new String[1000], n2[] = new String[1000];
String template_id = model.getTemplate_id();
//
// for (int i = 0; i < dataList.size(); i++) {
//
// n1[i] = template_id.split(",")[i];
// Log.d("values", n1[i]);
// }
////
// n1[position] = template_id.split(",")[0];
// n2[position] = template_id.split(",")[1];
//
// Log.d("values", n1[position]);
List<String> items = Arrays.asList(template_id.split("\\s*,\\s*"));
Log.d("items", items.get(0));
if (model.getTemplate_id().contains("10")) {
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
}
if (model.getTemplate_id().contains("9")) {
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
holder.mBinding.tvMembership.setText(model.getNo_of_locations() + " Locations");
holder.mBinding.tvPrice.setText(model.getStart_to_end());
}
if (model.getTemplate_id().contains("8")) {
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
holder.mBinding.tvMembership.setText(model.getVenue());
holder.mBinding.tvPrice.setText(model.getStart_to_end());
}
if (model.getTemplate_id().contains("7")) {
holder.mBinding.ivPrice.setImageResource(R.drawable.ic_reward_dollar);
holder.mBinding.tvPrice.setText(model.getPrice());
holder.mBinding.tvPrice.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.GONE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("6")) {
holder.mBinding.ivPrice.setImageResource(R.drawable.ic_reward_point);
holder.mBinding.tvPrice.setText(model.getPoints());
holder.mBinding.tvPrice.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.GONE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("5")) {
holder.mBinding.tvMembership.setText(R.string.almost_out_of_stock);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_selling_fast);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("4")) {
holder.mBinding.tvMembership.setText(R.string.selling_fast);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_selling_fast);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.tab_selected));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("3")) {
holder.mBinding.tvMembership.setText(R.string.gold_exclusive);
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_gold_exclusive);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.colorGold));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("2")) {
holder.mBinding.tvPrice.setText(mCMSTextData.getBirthday());
holder.mBinding.ivPrice.setImageResource(R.drawable.ic_birthday);
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("1")) {
holder.mBinding.tvMembership.setText(R.string.out_of_stock);
holder.mBinding.tvPrice.setText(model.getStart_to_end());
holder.mBinding.ivMembership.setImageResource(R.drawable.ic_out_of_stock);
holder.mBinding.tvMembership.setTextColor(context.getResources().getColor(R.color.colorAccent));
holder.mBinding.llMembership.setVisibility(View.VISIBLE);
holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
}
if (model.getTemplate_id().contains("0")) {
holder.mBinding.llMembership.setVisibility(View.GONE);
holder.mBinding.llPrice.setVisibility(View.GONE);
}
//
// if (model.getCatalogue_type().equals("2") || model.getCatalogue_type().equals("3")) {
//
// holder.mBinding.llMembership.setVisibility(View.VISIBLE);
// holder.mBinding.tvPrice.setVisibility(View.VISIBLE);
//
// holder.mBinding.ivPrice.setImageResource(R.drawable.ic_clock_black);
// holder.mBinding.ivMembership.setImageResource(R.drawable.ic_location_catalogue);
//
// }
}
}
@Override
public int getItemCount() {
return dataList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private final RowCatalogueBinding mBinding;
ViewHolder(RowCatalogueBinding itemLayoutView) {
super(itemLayoutView.getRoot());
this.mBinding = itemLayoutView;
mBinding.llMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onViewClick(v, getAdapterPosition());
}
}
});
}
}
}
Модель:
public class CatalogueData
{
private String catalogue_type;
private String unique_code;
private String name;
private String short_description;
private String long_description;
private String terms_conditions;
private String image4x3;
private String image16x9;
private String created_date;
private String is_mercatus_group;
private String is_featured;
private String reward_type;
private String start_date;
private String end_date;
private List<String> periods = null;
private String points;
private String price;
private String no_of_locations;
private List<String> address = null;
private String selling_lable;
private String mall_id;
private List<Object> schedule = null;
private String template_id;
private String venue;
private String level_logo_image;
private String start_to_end;
private String is_birthday;
private String gold_exclusive;
private String price_value;
private String points_value;
}
Пожалуйста, предложите что-нибудь.Дайте мне знать, если вам требуется какой-либо код.