• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Add a second brand. Please help.

carelv7

Newbie
Jul 4, 2017
18
1
Hi, thanks for any help in advance. I build this recyclerview so far and it works perfect. It takes the pallet size and times it by the cases, it keeps the data in the textview if I scroll out of screen, just as I want it. Now I want to add a second and third brand (Castle Lager and whatever), but if I want to import it, like the Black Label, I cant. (Only learned that now I cant do more than one in a single static import). Please help with any suggestions on how I can add more brands without having to do most of the code all over.
Here is the BlackLabel class:

Java:
public class BlackLabel extends brands {

    private RecyclerView recyclerView;
    //private String modelArrayList;
    //private Context ctx;
    public static ArrayList<Model> modelArrayList;
    private CustomAdapter customAdapter;
    //private Button btnnext;
    public String[] brandlist = new String[]{
            "Black Label 340ml NRB (85023)",
            "Black Label 330ml Cans (85736)",
            "Black label 500ml Cans (85023)",
            "Black Label 440ml NRB (86798)",
            "Black Label 330ml RB (85556)",
            "Black Label 750ml RB (85021)",
            "Black Label 340ml NRB 12 Pack (87009)",
            "Black Label 500ml Cans 12 Pack (85022)"};

    public int[] pallet_size = {
            84,
            127,
            81,
            80,
            120,
            70,
            132,
            90};

    //int palletsize = Integer.parseInt(String.valueOf(pallet_size));

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_by_brand);
        setTitle("Black Label");


        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        //btnnext = (Button) findViewById(R.id.next);

        modelArrayList = getModel();
        customAdapter = new CustomAdapter(this);
        recyclerView.setAdapter(customAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

    }

    private ArrayList<Model> getModel() {
        ArrayList<Model> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {

            Model model = new Model();
            model.setNumber(0);
            model.setNumber2(0);
            model.setBrand(brandlist[i]);
            model.setPallet_size(pallet_size[i]);
            list.add(model);
        }
        return list;
        // Jy het laas hier getrek. wat jy moet doen is om 'n textview gelyk te stel aan die position en dit so dan
        // met die getal pallets temaal.
    }
}

And here is my CustomAdapter:

Java:
import static android.os.Build.VERSION_CODES.M;
    //import java.util.modelArraylist;
    import static com.example.android.sab.BlackLabel.modelArrayList;
    //import static com.example.android.sab.CastleLager.modelArrayList;

/**
* Created by Carel on 2017/08/15.
*/

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {


    private LayoutInflater inflater;
    private Context ctx;

    public CustomAdapter(Context ctx) {

        inflater = LayoutInflater.from(ctx);
        this.ctx = ctx;
    }

    @Override
    public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.rv_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {

        holder.tvBrand.setText((CharSequence) modelArrayList.get(position).getBrand());
        holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber()));
        holder.tvPallet_size.setText(String.valueOf(modelArrayList.get(position).getPallet_size()));
        holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber2()));
        holder.tvPallets.setText(String.valueOf(modelArrayList.get(position).getNumber()));
        holder.tvCases.setText(String.valueOf(modelArrayList.get(position).getNumber2()));
    }

    @Override
    public int getItemCount() {
        return modelArrayList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        protected Button btn_cases_plus, btn_cases_minus, btn_pallets_plus, btn_pallets_minus;
        private TextView tvBrand, tvCases,tvPallets, tvPallet_size;

        public MyViewHolder(View itemView) {
            super(itemView);

            tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
            tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
            tvPallet_size = (TextView) itemView.findViewById(R.id.pallets_size);
            tvPallets = (TextView) itemView.findViewById(R.id.pallets_text_view);
            btn_cases_plus = (Button) itemView.findViewById(R.id.casePlus1);
            btn_cases_minus = (Button) itemView.findViewById(R.id.caseMinus1);
            btn_pallets_plus = (Button) itemView.findViewById(R.id.palletsPlus1);
            btn_pallets_minus = (Button) itemView.findViewById(R.id.palletsMinus1);

            btn_cases_plus.setTag(R.integer.btn_cases_plus_view, itemView);
            btn_cases_minus.setTag(R.integer.btn_cases_minus_view, itemView);
            btn_cases_plus.setOnClickListener(this);
            btn_cases_minus.setOnClickListener(this);
            btn_pallets_plus.setTag(R.integer.btn_pallets_plus_view, itemView);
            btn_pallets_minus.setTag(R.integer.btn_pallets_minus_view, itemView);
            btn_pallets_plus.setOnClickListener(this);
            btn_pallets_minus.setOnClickListener(this);

        }

        // onClick Listener for view
        @Override
        public void onClick(View v) {

            if (v.getId() == btn_cases_plus.getId()){

                View tempview = (View) btn_cases_plus.getTag(R.integer.btn_cases_plus_view);
                TextView tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
                int number = Integer.parseInt(tvCases.getText().toString()) + 1;
                tvCases.setText(String.valueOf(number));
                modelArrayList.get(getAdapterPosition()).setNumber(number);

            } else if(v.getId() == btn_cases_minus.getId()) {

                View tempview = (View) btn_cases_minus.getTag(R.integer.btn_cases_minus_view);
                TextView tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
                int number = Integer.parseInt(tvCases.getText().toString()) - 1;
                if (number == 0 || number <0) {
                    Toast.makeText(ctx,"You cannot order less than one case", Toast.LENGTH_SHORT).show();
                    tvCases.setText(String.valueOf(0));
                    return;
                }
                tvCases.setText(String.valueOf(number));
                modelArrayList.get(getAdapterPosition()).setNumber(number);

            }  else if(v.getId() == btn_pallets_plus.getId()) {

                View tempview = (View) btn_pallets_plus.getTag(R.integer.btn_pallets_plus_view);
                TextView tvPallets = (TextView) tempview.findViewById(R.id.pallets_text_view);
                TextView tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
                TextView tvPallet_size = (TextView) tempview.findViewById(R.id.pallets_size);
                int number = Integer.parseInt(tvPallets.getText().toString()) + 1;
                tvPallets.setText(String.valueOf(number));
                int number2 = Integer.parseInt(tvPallets.getText().toString()) * Integer.parseInt(tvPallet_size.getText().toString());
                tvCases.setText(String.valueOf(number2));
                modelArrayList.get(getAdapterPosition()).setNumber(number);
                modelArrayList.get(getAdapterPosition()).setNumber2(number2);

            }   else if(v.getId() == btn_pallets_minus.getId()) {

                View tempview = (View) btn_pallets_minus.getTag(R.integer.btn_pallets_minus_view);
                TextView tvPallets = (TextView) tempview.findViewById(R.id.pallets_text_view);
                TextView tvCases = (TextView) tempview.findViewById(R.id.cases_text_view);
                TextView tvPallet_size = (TextView) tempview.findViewById(R.id.pallets_size);
                int number = Integer.parseInt(tvPallets.getText().toString()) - 1;
                tvPallets.setText(String.valueOf(number));
                int number2 = Integer.parseInt(tvPallets.getText().toString()) * Integer.parseInt(tvPallet_size.getText().toString());
                tvCases.setText(String.valueOf(number2));
                if (number == 0 || number <0) {
                    Toast.makeText(ctx,"You cannot order less than one pallet", Toast.LENGTH_SHORT).show();
                    tvCases.setText(String.valueOf(0));
                    tvPallets.setText(String.valueOf(0));
                }
                modelArrayList.get(getAdapterPosition()).setNumber(number);
                modelArrayList.get(getAdapterPosition()).setNumber2(number2);
            }

        }

    }
}

And I would like to use this CastleLager ArrayList (which I cant):

Java:
public class CastleLager extends brands {

    private RecyclerView recyclerView;
    //private String modelArrayList;
    //private Context ctx;
    public static ArrayList<Model> modelArrayList;
    private CustomAdapter customAdapter;
    //private Button btnnext;
    public String[] brandlist = new String[]{
            "Castle Lager 340ml NRB (85023)",
            "Castle Lager 330ml Cans (85736)",
            "Castle Lager 500ml Cans (85023)",
            "Castle Lager 440ml NRB (86798)",
            "Castle Lager 330ml RB (85556)",
            "Castle Lager 750ml RB (85021)",
            "Castle Lager 340ml NRB 12 Pack (87009)",
            "Castle Lager 500ml Cans 12 Pack (85022)"};

    public int[] pallet_size = {
            84,
            127,
            81,
            80,
            120,
            70,
            132,
            90};

    //int palletsize = Integer.parseInt(String.valueOf(pallet_size));

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_by_brand);
        setTitle("Castle Lager");


        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        //btnnext = (Button) findViewById(R.id.next);

        modelArrayList = getModel();
        customAdapter = new CustomAdapter(this);
        recyclerView.setAdapter(customAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
    }

    private ArrayList<Model> getModel() {
        ArrayList<Model> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {

            Model model = new Model();
            model.setNumber(0);
            model.setNumber2(0);
            model.setBrand(brandlist[i]);
            model.setPallet_size(pallet_size[i]);
            list.add(model);
        }
        return list;
    }
}
 
Thank you LV. I know, I'm brand new to this Android thing, but being my first project after a tutorial or two on Udacity it can't be all that bad. I'm not into databases yet and will get there soon. All I want for now is to add a second and third brand, doesn't matter if its a lot of duplication. (The end goal is to convert values the text fields into an email and mail it.)

Is a database the only way to do it? And if so, where do I start?
 
Upvote 0
I'm not knocking your code, it's great that you managed to build this app. But really you should only hardcode things like this as a very basic prototype. If you want to extend it, as you have found, then the code becomes messy and unmaintainable.

Learning about databases is really vital for you because most apps have some kind of persistent storage requirement for their data, and 90% of the time, using the database is the best way of doing it. There are many web resources, and Google have recently introduced an ORM framework called ROOMS (thanks @steve). So if you're coming fresh to learning about databases, then I'd start with this. It makes a lot of sense, because you can directly map the data objects in your code to database tables, and the framework handles all the boiler plate code of getting data in and out of the database.

Here's a good link to start with

http://www.vogella.com/tutorials/AndroidSQLite/article.html
 
  • Like
Reactions: steve
Upvote 0
I knew I had to learn databases at some stage, but it looks like I have to do it sooner than later. I followed your link and will tackle it after I'm done with this thing. I'm just going to finish this with only one brand for the sake of completeness. All I still need to do is to convert the textfields into an email and mail it. Maybe its pointless, but it's just me.

Thank you again LV for all your help so far.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones