1. Are you ready for the Galaxy S20? Here is everything we know so far!

Adapter not finding ArrayList

Discussion in 'Android Development' started by carelv7, Aug 7, 2017.

  1. carelv7

    carelv7 Newbie
    Thread Starter

    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:
    Code (Java):
    1. public class MainActivity extends AppCompatActivity {
    2.  
    3.     private RecyclerView recyclerView;
    4.     //public static ArrayList<Model> modelArrayList;
    5.     private CustomAdapter customAdapter;
    6.     private Button btnnext;
    7.     public static ArrayList brandlist;
    8.  
    9.     public static void main(String[] args) {
    10.         ArrayList brandlist = new ArrayList<>();
    11.  
    12.             brandlist.add("340ml NRB (85023)");
    13.             brandlist.add("330ml Cans (85736)");
    14.             brandlist.add("500ml Cans (85024)");
    15.             brandlist.add("440ml NRB (86798)");
    16.             brandlist.add("330ml RB (85556)");
    17.             brandlist.add("750ml RB (85021)");
    18.             brandlist.add("340ml NRB 12 pack (87009)");
    19.             brandlist.add("500ml Cans 12 Pack (85022)");
    20. }
    21.  
    22.     protected void onCreate(Bundle savedInstanceState) {
    23.         super.onCreate(savedInstanceState);
    24.         setContentView(R.layout.activity_main);
    25.  
    26.         recyclerView = (RecyclerView) findViewById(R.id.recycler);
    27.         btnnext = (Button) findViewById(R.id.next);
    28.  
    29.         brandlist = getModel();
    30.         customAdapter = new CustomAdapter(this);
    31.         recyclerView.setAdapter(customAdapter);
    32.         recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
    33.  
    34.         btnnext.setOnClickListener(new View.OnClickListener() {
    35.             @Override
    36.             public void onClick(View v) {
    37.                 Intent intent = new Intent(MainActivity.this,NextActivity.class);
    38.                 startActivity(intent);
    39.             }
    40.         });
    41.     }
    42.  
    43.     private ArrayList<Model> getModel(){
    44.         ArrayList<Model> list = new ArrayList<>();
    45.         for(int i = 0; i < 8 ; i++){
    46.  
    47.             Model model = new Model();
    48.             model.setNumber(0);
    49.             model.setBrandlist(brandlist);
    50.             list.add(model);
    51.         }
    52.         return list;
    53.     }
    54. }
    55.  
    And here is my adapter:
    Code (Java):
    1. public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder> {
    2.  
    3.     private LayoutInflater inflater;
    4.     private Context ctx;
    5.  
    6.     public CustomAdapter(Context ctx) {
    7.  
    8.         inflater = LayoutInflater.from(ctx);
    9.         this.ctx = ctx;
    10.     }
    11.  
    12.     @Override
    13.     public CustomAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    14.  
    15.         View view = inflater.inflate(R.layout.rv_item, parent, false);
    16.         MyViewHolder holder = new MyViewHolder(view);
    17.  
    18.         return holder;
    19.     }
    20.  
    21.     @Override
    22.     public void onBindViewHolder(final CustomAdapter.MyViewHolder holder, int position) {
    23.  
    24.         holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getbrandlist());
    25.         holder.tvCases.setText(String.valueOf(MainActivity.brandlist.get(position).getNumber()));
    26.         holder.tvPallets.setText(String.valueOf(MainActivity.brandlist.get(position).getNumber()));
    27.  
    28.     }
    29.  
    30.     @Override
    31.     public int getItemCount() {
    32.         return MainActivity.brandlist.size();
    33.     }
    34.  
    35.     class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    36.  
    37.         protected Button btn_cases_plus, btn_cases_minus, btn_pallets_plus, btn_pallets_minus;
    38.         private TextView tvBrand, tvCases,tvPallets;
    39.  
    40.         public MyViewHolder(View itemView) {
    41.             super(itemView);
    42.  
    43.             tvBrand = (TextView) itemView.findViewById(R.id.brand_name);
    44.             tvCases = (TextView) itemView.findViewById(R.id.cases_text_view);
    45.             tvPallets = (TextView) itemView.findViewById(R.id.pallets_text_view);
    46.             btn_cases_plus = (Button) itemView.findViewById(R.id.casePlus1);
    47.             btn_cases_minus = (Button) itemView.findViewById(R.id.caseMinus1);
    48.             btn_pallets_plus = (Button) itemView.findViewById(R.id.palletsPlus1);
    49.             btn_pallets_minus = (Button) itemView.findViewById(R.id.palletsMinus1);
    50.  
    51.             btn_cases_plus.setTag(R.integer.btn_cases_plus_view, itemView);
    52.             btn_cases_minus.setTag(R.integer.btn_cases_minus_view, itemView);
    53.             btn_cases_plus.setOnClickListener(this);
    54.             btn_cases_minus.setOnClickListener(this);
    55.             btn_pallets_plus.setTag(R.integer.btn_pallets_plus_view, itemView);
    56.             btn_pallets_minus.setTag(R.integer.btn_pallets_minus_view, itemView);
    57.             btn_pallets_plus.setOnClickListener(this);
    58.             btn_pallets_minus.setOnClickListener(this);
    59.  
    60.         }
    61.  
    62.         // onClick Listener for view
    63.         @Override
    64.         public void onClick(View v) {
    65.  
    66.             if (v.getId() == btn_cases_plus.getId()){
    67.  
    68.                 View tempview = (View) btn_cases_plus.getTag(R.integer.btn_cases_plus_view);
    69.                 TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
    70.                 int number = Integer.parseInt(tv.getText().toString()) + 1;
    71.                 tv.setText(String.valueOf(number));
    72.                 MainActivity.brandlist.get(getAdapterPosition()).setNumber(number);
    73.  
    74.             } else if(v.getId() == btn_cases_minus.getId()) {
    75.  
    76.                 View tempview = (View) btn_cases_minus.getTag(R.integer.btn_cases_minus_view);
    77.                 TextView tv = (TextView) tempview.findViewById(R.id.cases_text_view);
    78.                 int number = Integer.parseInt(tv.getText().toString()) - 1;
    79.                 tv.setText(String.valueOf(number));
    80.                 MainActivity.brandlist.get(getAdapterPosition()).setNumber(number);
    81.             }
    82.         }
    83.  
    84.     }
    85. }
    86.  
    and here is my model where I tried to declare the ArrayList:
    Code (Java):
    1. public class Model {
    2.  
    3.     private int number;
    4.     private ArrayList brandlist;
    5.  
    6.     public int getNumber() {
    7.         return number;
    8.     }
    9.  
    10.     public void setNumber(int number) {
    11.         this.number = number;
    12.     }
    13.  
    14.     public ArrayList getBrandlist() {
    15.         return brandlist;
    16.     }
    17.  
    18.     public void setBrandlist(ArrayList brandlist) {
    19.         this.brandlist = brandlist;
    20.     }
    21. }
     


  2. Best Answer:
    Post #2 by Deleted User, Aug 7, 2017 (3 points)

    1. Download the Forums for Android™ app!


      Download

       
  3. Deleted User

    Deleted User Guest

    You've typed

    Code (Text):
    1.  
    2. holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getbrandlist());
    3.  
    and it should be

    Code (Text):
    1.  
    2. holder.tvBrand.setText((CharSequence) MainActivity.brandlist.get(position).getBrandlist());
    3.  
    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?
     
    scary alien likes this.
  4. scary alien

    scary alien not really so scary
    VIP Member

    Great answer and tip there @LV426 :)
     
  5. carelv7

    carelv7 Newbie
    Thread Starter

    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
     
  6. Deleted User

    Deleted User Guest

    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.
     
  7. Deleted User

    Deleted User Guest

    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().
     
  8. Deleted User

    Deleted User Guest

    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?
     
    scary alien likes this.
  9. carelv7

    carelv7 Newbie
    Thread Starter

    No
    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.
     
Loading...

Share This Page

Loading...