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++) {