Logic for strings with saperated commas with different values in two textviews

Related searches

I have one key with string with comma separated values. i.e. "template_id": "9,10" / "template_id": "0". From API I'm getting 10 different parameters for each ids.

 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"

I am using Recyclerview. I have added below code for checking ids and add textview settext according the ids:

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);
        }

But every time this textview got override by other values. I have tried by taking 20 TextView and conditions but still not working.

============ EDIT ==============

Adapter Class :

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());
                    }
                }
            });

        }
    }


}

Model :

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;
}

Please suggest anything. Let me know if you require any code.

Keep all TextView separate then save all template_id in ArrayList with split then use condition, I'm sure it will work. Please check below code snippet.

    String template_id = model.getTemplate_id();
    ArrayList<String> templateIDs = new ArrayList<>();
    StringTokenizer templateTokenizer = new StringTokenizer(template_id, ",");
    while(templateTokenizer.hasMoreTokens()) {
        templateIDs.add(templateTokenizer.nextToken());
    }

    if(templateIDs.contains("0")) {
        // Do your work according 0 value
    }

    if(templateIDs.contains("1")) {
        // Do your work according 1 value
    }

    if(templateIDs.contains("2")) {
        // Do your work according 2 value
    }

What is the better way to add commas to multiple elements string , I need to put comma separated values into ourOutput (for future output). So, what I need is to add commas and remove last unnecessary comma� Given an input string that is comma-separated instead of space. The task is to store this input string in a list or variables. This can be achieved in Python using two ways: Using List comprehension and split() Using map() and split() Method 1: Using List comprehension and split() split() function helps in getting multiple inputs from the user. It breaks the given input by the specified separator.

Try using

.equals("YOUR_ID_HERE")

instead of

.contains("YOUR_ID_HERE")

and see if that helps

How to split or display a long text string in textview to sections or , How would i be able to display a long string of text in a textview on android, app is displayed in various sections because the text strings are long. on string over x amounts of characters in length (or some other logic). Splits the words apart, using a comma as the separator. The second result starts with a space since this was the character immediately following the comma. Split( "Hello, World", "o" ) Splits the string apart, using the character "o" as the separator. Split( "Hello, World", "l" ) Splits the string apart, using the single character "l" as the separator.

Loop the items arraylist instead,

Iterator i = items.iterator();

  while (i.hasNext()) {
     if(items[i].equals("9"){
          //write your logic here
     }
     if(items[i].equals("10"){
          //write your logic here
     }
     if(items[i].equals("1"){
          //write your logic here
     }
     //and so on write your every condition

  }

Convert a Set of String to a comma separated String in Java , How to Convert a Byte value to String value in Java with Examples � RishabhPrabhu. Check out this Author's contributed articles. If you like� // Split the comma separated list to an array that will contain the words you want. string[] words = commaSeparatedList.Split(','); // Iterate through the words in array called words foreach(string word in words) { // Code for insert the word in your database.

Looks like you're splitting the template_id but then calling contains on the full un-split string.

If you received template_id("10,6") then the if statements would be called for 10, 6, 1 and 0.

Only guessing as I don't know the context but I imagine you actually want to use the split values and check each value individually. Also I would imagine you want to use an if-else-if ladder rather than sequential if statements.

Support different languages and cultures, It's a good practice to keep culture-specific resources separated from the There are also a few default files such as res/values/strings.xml Figure 2. Icon used for the default (en_US) locale. Spain's flag icon TextView(this).apply { your layout should use the RTL text direction, use the following logic:. Given an input string which is comma-separated instead of space, the task is to parse this input string in C++. First, let us understand what difference does it create if the input string is comma-separated. Taking input a whitespace-separated string. Taking input a whitespace-separated string in C++ is very easy. The program to do so is:

Read Data From Text File, I have multiple text files that contain data in this format subID imageCondition myFile of type String, text of type String, textline of type String, posLat of type To load a CSV (Comma Separated Values) file, we specify delimitter to “,”. Table elements by using various heuristics to recognize the PDF file's logical structure. The result may be achieved in two different ways. Method 1: Count digits and extract that many chars. The easiest way to split text string where number comes after text is this: To extract numbers, you search the string for every possible number from 0 to 9, get the numbers total, and return that many characters from the end of the string.

C# program to convert several strings into a single comma-delimited string; Replacing numbers on a comma delimited result with MySQL? Combining multiple rows into a comma delimited list in MySQL? PHP program to split a given comma delimited string into an array of values; MySQL select distinct rows into a comma delimited list column?

Char *strings = "1,5,95,255" I want to store each number into an int variable and then print it out. For example the output becomes like this. Value1 = 1. value2 = 5. Value3= 95 . value4 = 255. And I want to do this inside a loop, so If I have more than 4 values in strings, I should be able to get the rest of the values.

Comments
  • post your adapter class and model object, would be helpful.
  • @Sajith i have edited the question. please check
  • @Annie : you are using one same textview to set the text for all values?
  • @UzairAslam Yes but i also tried using 20 textviews. let me edit the code.
  • @Annie what you want? Do you want to display all value at same time or only one value ?
  • This should be comment instead of answer.
  • @NiranjPatel I do not have enough reputation to comment everywhere pal
  • @Annie You could check for 9,10 for that case also you could convert your if statements into a switch case