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

Adapter not finding ArrayList

carelv7

Newbie
Jul 4, 2017
18
1
Hi, I hope everybody is still doing well. I'm building (still lol) an order app. I changed some stuff to make my string an arraylist, now it doesn't want to get my ArrayList. If I run the app it tells me it cant resolve symbol "getBrandList". Please help me.
mainActivity:
Java:
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    //public static ArrayList<Model> modelArrayList;
    private CustomAdapter customAdapter;
    private Button btnnext;
    public static ArrayList brandlist;

    public static void main(String[] args) {
        ArrayList brandlist = new ArrayList<>();

            brandlist.add("340ml NRB (85023)");
            brandlist.add("330ml Cans (85736)");
            brandlist.add("500ml Cans (85024)");
            brandlist.add("440ml NRB (86798)");
            brandlist.add("330ml RB (85556)");
            brandlist.add("750ml RB (85021)");
            brandlist.add("340ml NRB 12 pack (87009)");
            brandlist.add("500ml Cans 12 Pack (85022)");
}

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

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

        btnnext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                startActivity(intent);
            }
        });
    }

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

            Model model = new Model();
            model.setNumber(0);
            model.setBrandlist(brandlist);
            list.add(model);
        }
        return list;
    }
}

And here is my adapter:
Java:
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) MainActivity.brandlist.get(position).getbrandlist());
        holder.tvCases.setText(String.valueOf(MainActivity.brandlist.get(position).getNumber()));
        holder.tvPallets.setText(String.valueOf(MainActivity.brandlist.get(position).getNumber()));

    }

    @Override
    public int getItemCount() {
        return MainActivity.brandlist.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;

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

            tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
            tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
            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 tv = (TextView) tempview.findViewById(R.id.cases_text_view);
                int number = Integer.parseInt(tv.getText().toString()) + 1;
                tv.setText(String.valueOf(number));
                MainActivity.brandlist.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 tv = (TextView) tempview.findViewById(R.id.cases_text_view);
                int number = Integer.parseInt(tv.getText().toString()) - 1;
                tv.setText(String.valueOf(number));
                MainActivity.brandlist.get(getAdapterPosition()).setNumber(number);
            }
        }

    }
}
and here is my model where I tried to declare the ArrayList:
Java:
public class Model {

    private int number;
    private ArrayList brandlist;

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public ArrayList getBrandlist() {
        return brandlist;
    }

    public void setBrandlist(ArrayList brandlist) {
        this.brandlist = brandlist;
    }
}
 
You've typed

Code:
holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getbrandlist());

and it should be

Code:
holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getBrandlist());

Incidentally, Android Studio can do code completion, which helps you to get the correct method name. Yes, it can actually write the code for you!
On the above line if you type the first part of the method name "get", then hit CTRL-SPACE, it will come up with some method name suggestions, and if unique, will fill in the method name for you. You can even set it to auto code completion. Cool eh?
 
  • Like
Reactions: scary alien
Upvote 0
You've typed

Code:
holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getbrandlist());

and it should be

Code:
holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getBrandlist());

Incidentally, Android Studio can do code completion, which helps you to get the correct method name. Yes, it can actually write the code for you!
On the above line if you type the first part of the method name "get", then hit CTRL-SPACE, it will come up with some method name suggestions, and if unique, will fill in the method name for you. You can even set it to auto code completion. Cool eh?
You've typed

Code:
holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getbrandlist());

and it should be

Code:
holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getBrandlist());

Incidentally, Android Studio can do code completion, which helps you to get the correct method name. Yes, it can actually write the code for you!
On the above line if you type the first part of the method name "get", then hit CTRL-SPACE, it will come up with some method name suggestions, and if unique, will fill in the method name for you. You can even set it to auto code completion. Cool eh?

Thank you very much so far. At least it loads the app now, but it doesn't display the brand list. It shows everything but not the brand list. the buttons also work. I think it has something to do with my array list.
Here is a screenshot:
Screenshot_20170807-161605.png
 
Upvote 0
Ah well this is where you have to roll up your sleeves and do some code debugging. Can be tedious, but quite often things don't run as you would expect.
Can I recommend that you familiarise yourself with running your application in debug mode, and setting breakpoints in the code? This can help you track down why things aren't working as expected. Sorry to be a little unhelpful, but interactive debugging is often impractical in a Q&A technical forum.
I will study the code and see if I can work it out, but really you'll be doing yourself a massive favour by learning about debug techniques.
 
Upvote 0
First thing that's odd about your code is that it declares a main() method, in which you initialise the brandlist Array. Android apps are not stand alone Java applications, and I would be extremely surprised if that main() method is ever called. Therefore your brandlist Array will remain empty.
You should put initialisation code like this in onCreate().
 
Upvote 0
No
To verify the above, try this - run your app in debug mode, and set a breakpoint in the main() method. Does the breakpoint ever get hit?
No it does not. Breaks if I put it before, and it breaks if I put it after, but not in the main method, just opens the app. So that means it doesn't see my ArrayList which has my products in it, hence no products as per the pic.
 
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