1

My program has a main activity with two spinners. They both contain station names ( the same ones ). In the second activity I created a custom ListView, which has two text views side by side on one row. In the first text view you have a number and in the second text view there are the names of the stations. But that's not the important part. Now, the problem is that the stations should be introduced from an array. Everything worked fine when I had a pre-defined array ( the station names were already in there, placed by me ).

But then I changed the code to make an empty array. I also added two arrays with values inside them. I wanted to have this empty array in order to add certain values from these other arrays inside it. And then, the contents of this array would be displayed inside the listview.

public class SecondActivity extends AppCompatActivity {
   ListView myList;
   ArrayList<String> list1;
   ArrayList<String> list2;
   String[] yellow = { "Station 1", "Station 2", "Station 3", "Station 4", "Station 5", "Station 6" };
   String[] green = { "Station 1.1", "Station 2.1", "Station 3.1", "Station 4.1", "Station 5.1",
         "Station 6.1" };

   int i, x, startPoint, finishPoint;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_fourth);

      Intent intent = getIntent();
      // Here I am getting values from the previous activity
      String myvalue1 = intent.getStringExtra("value1"); // Here I get the value of the line on which the first station
                                                         // is selected (like "M1" or "M2")
      String myvalue2 = intent.getStringExtra("value2"); // Here I get the value of the second line on which the final
                                                         // station is selected (like "M1" or "M2")
      String mystart = intent.getStringExtra("start"); // Here I get the actual name of the first station
      String myfinish = intent.getStringExtra("finish"); // Here I get the actual name of the final station

      ArrayList<String> list1 = new ArrayList<>();
      ArrayList<String> list2 = new ArrayList<>();
      if (myvalue1.equals(myvalue2)) {
         if (myvalue1 == "M1" && myvalue2 == "M1") { // Everything here is fine
            for (i = 0; i <= yellow.length; i++) {
               if (mystart == yellow[i]) {
                  startPoint = i;
               }
               if (myfinish == yellow[i]) {
                  finishPoint = i;
               }
            }
            for (x = startPoint; x <= finishPoint; x++) {
               list1.add(yellow[x]);
               list2.add("Line 1");
            }
         }
      }

      MyListAdapter adapter = new MyListAdapter(this, list1, list2);
      myList = (ListView) findViewById(R.id.myList);
      myList.setAdapter(adapter);
   }
}

EDIT: This is the Custom Adapter Code. As I said, the list uses two text views, so therefore there are two lists. In the first textview/arraylist it should be shown the line on which the station is and the second it should show the name of the station.

public class MyListAdapter extends ArrayAdapter<String> {
   private final Activity context;
   private final ArrayList<String> list1;
   private final ArrayList<String> list2;

   public MyListAdapter(Activity context, ArrayList<String> list1, ArrayList<String> list2) {
      super(context, R.layout.mylist, list1);

      this.context = context;
      this.list1 = list1;
      this.list2 = list2;
   }

   public View getView(int position, View view, ViewGroup parent) {
      LayoutInflater inflater = context.getLayoutInflater();
      View rowView = inflater.inflate(R.layout.mylist, null, true);
      TextView textView = (TextView) rowView.findViewById(R.id.textView);
      TextView textView2 = (TextView) rowView.findViewById(R.id.textView2);

      textView.setText(list1.get(position));
      textView2.setText(list2.get(position));

      return rowView;
   }
}

EDIT 2: Here I have added the logcat. It shows that the array index is out of bounds. Getting closer and closer to getting the answer.

    2020-05-11 22:57:25.177 13084-13084/com.example.myapplication 
D/AndroidRuntime: Shutting down VM
    2020-05-11 22:57:25.180 13084-13084/com.example.myapplication 
       E/AndroidRuntime: FATAL EXCEPTION: main
       Process: com.example.myapplication, PID: 13084
       java.lang.ArrayIndexOutOfBoundsException: length=21; index=21 at com.example.myapplication.SecondActivity$3.onClick(SecondActivity.java:127)
       at android.view.View.performClick(View.java:7356)
       at android.view.View.performClickInternal(View.java:7333)
       at android.view.View.access$3600(View.java:807)
       at android.view.View$PerformClick.run(View.java:28200)
       at android.os.Handler.handleCallback(Handler.java:907)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:223)
       at android.app.ActivityThread.main(ActivityThread.java:7476)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:939)
2020-05-11 22:57:25.270 13084-13084/? I/Process: Sending signal. PID: 13084 SIG: 9

SOLUTION: The first for loop needs to be changed from for (i = 0; i <= yellow.length; i++) { to for (i = 0; i <= (yellow.length)-1; i++) {

Johnny Uke
  • 71
  • 2
  • 9
  • 1
    Change `list1` to an `ArrayList` and remember to initialise it before the loop with `ArrayList list1 = new ArrayList<>();`. – codebod May 10 '20 at 14:28
  • Thank you @codebod So, I have to declare it like this: ArrayList list1; And another question: Do I also need to change list1 inside the CustomAdapter.java file? Because list1 appears there as well, but as String[] list1 as well. – Johnny Uke May 10 '20 at 15:07
  • 1
    Very likely, yes. Post the adapter as well and I'll advise on any specific changes if you need help. – codebod May 10 '20 at 15:58
  • 1
    Separately, you should change `myvalue1 == myvalue2` to `myvalue1.equals(myvalue2)`. These do not have the same meaning. See: https://stackoverflow.com/a/513839/13373270. In your code, `myvalue1` does not equal `myvalue2` every time. – codebod May 10 '20 at 16:01
  • Thank you @codebod I have changed String[] list1; to ArrayList list1; I also added the ListAdapter code. – Johnny Uke May 10 '20 at 16:36
  • There also seems to be a problem: when I declare ArrayList list1; after the public class and then I initialise it before the loop, it changes color from bold blue to grey ( if I hover my mouse over it, it says that list1 is never used ). It doesn't show an error. It is just something I found to be odd. – Johnny Uke May 10 '20 at 16:46
  • 1
    Could you amend your SecondActivity code above so that I can see exactly what you've done? Also, where has `list2` come from? – codebod May 10 '20 at 16:54
  • Sorry, I forgot to modify it. Here is what I did. – Johnny Uke May 10 '20 at 16:59
  • 1
    Now your `ArrayList`s are only being initialised if certain conditions are met. Move your 2 `new ArrayList<>()` statements up to before the first `if`. Otherwise you will be passing nulls into your adapter unless certain conditions are met, but I think you want empty Lists – codebod May 10 '20 at 17:31
  • Yes, I want empty lists so that afterwards I can populate them with items. Ok, I will move them before the first if statement. – Johnny Uke May 10 '20 at 17:48
  • @codebod I did as you said. Unfortunately, when I press the button to open the listview in a new activity, it shows only a white screen, without any items in the listview. Now, I'm thinking if the problem may be at the spinners. If I have sent the correct values to this activity. – Johnny Uke May 10 '20 at 18:20
  • 1
    Also, make sure you change things like `myvalue1 == "M1"` to `myvalue1.equals("M1")`. – codebod May 10 '20 at 18:21
  • When I do that and test the application it just goes back to the main activity. It doesn't work. – Johnny Uke May 10 '20 at 19:41
  • I found an error at the spinners. I'll try to fix it and then see if everything works. But tomorrow, because now it's a bit late. – Johnny Uke May 10 '20 at 19:57
  • Ok, I actually couldn't hold myself from correcting the error on the spinners. Now they work properly, but the listview still doesn't show up or it crashes. – Johnny Uke May 10 '20 at 20:27
  • 1
    Did you make sure you change things like `myvalue1 == "M1"` to `myvalue1.equals("M1")`? – codebod May 10 '20 at 21:13
  • 1
    Let's check that your `onCreate` method is working properly by looking at what your adapter constructor is receiving. After `super`, send to logcat the number of entries in each list with `list1.size()`, and if this isn't 0, iterate over each list and send the contents to logcat. Convince yourself that the adapter is receiving correct data. That will massively narrow the problem. – codebod May 10 '20 at 21:16
  • Thank you very much @codebod And I am so sorry for bothering you with all these questions. I have tried today. I have updated the post with what the logcat shows. I have moved all the code above ( except the custom adapter code ) to the activity with the spinners and tried displaying the list or at least the size of the list inside a toast in order to see if there is any problem with the listview. Turns out it isn't. The problem seems to be that the array index is out of bounds, as it says in the logcat. – Johnny Uke May 11 '20 at 20:01
  • 1
    It's no trouble. How have you implemented `onClick` in `SecondActivity` around line 127? – codebod May 11 '20 at 20:10
  • My friend, with your help, now everything works perfectly. I have changed ` for (i = 0; i <= yellow.length; i++) {` to `for (i = 0; i <= (yellow.length)-1; i++) {` . The reason is that I tested only for the yellow list, which in my code actually has 21 values instead of 6 that I displayed here. And as I started with i=0, automatically I should have decreased the length by 1. Again thank you very much! It's the first time I used logcat and it was very useful. – Johnny Uke May 11 '20 at 20:15

0 Answers0