0

I have an ArrayList where I store array of items of a specific field of a document, and I am sending it to another activity like this:

Intent intent = new Intent(ProgramToProgram.this, com.example.newapp.ProgramToProgram1.class);
intent.putExtra("fids", arrayList1);
startActivity(intent);

I recieve it in the other activity like this:

arraylist3 = (ArrayList<String>)getIntent().getSerializableExtra("fids");

Now I have another array list in this activty where I am storing another set of items:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        pid = docIds.get(position);

        fgboys1.whereEqualTo("pid", pid)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {

                        for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                            String fid = documentSnapshot.get("fid").toString();
                            arrayList1.add(fid);
                            arrayAdapter1.notifyDataSetChanged();
                        }

                        Toast.makeText(ProgramToProgram1.this, "FIND OUT REGISTERED OR NOT REGISTERED", Toast.LENGTH_SHORT).show();
                    }
                });
    }
});

Now I want to compare the items of arraylist3 and arraylist1 and return the number of items and the similar items. How do I do it?

This is what I have tried, but the app crashes each time:

registered.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int i;

        arraylist3 = (ArrayList<String>)getIntent().getSerializableExtra("fids");

        for (i = 0; i <arraylist3.size(); i++) {
            if (arraylist3.contains(arrayList1.get(i))) {
                i++;
            }
        }
        tv_reg.setText(i);
    }
});

this is the error shown in log :

  2020-02-12 11:50:19.014 16489-16489/com.example.newapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.newapp, PID: 16489
android.content.res.Resources$NotFoundException: String resource ID #0x0
    at android.content.res.Resources.getText(Resources.java:338)
    at android.widget.TextView.setText(TextView.java:5494)
    at com.example.newapp.ProgramToProgram1$4.onClick(ProgramToProgram1.java:215)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
jai
  • 41
  • 1
  • 8

1 Answers1

1

There are some clear bugs in this code:

public void onClick(View v) {
    int i;

    arraylist3 = (ArrayList<String>)getIntent().getSerializableExtra("fids");

    for (i = 0; i <arraylist3.size(); i++) {
        if (arraylist3.contains(arrayList1.get(i))) {
            i++;
        }
    }
    tv_reg.setText(i);
}

Problem #1: you are using i for two (conflicting) purposes: as a counter, and as a loop variable. That won't work.

Problem #2: you are appear to be using the wrong arrayList size in:

for (i = 0; i <arraylist3.size(); i++) {

versus

arrayList1.get(i)

If the two lists are not guaranteed to always have the same size, that is liable to either skip counting some elements, or give an index out of bounds exception (when i >= arrayList1.size()).


There are also some design problems;

  • Your variable names are uninformative: arrayList1 and arrayList3 say nothing about the meaning of the lists

  • The arrayList3 variable looks like it could / should be a local variable not a field.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • problem #1 i understood, i'll just make it - public void onClick(View v) { int i,r; Arraylist arraylist3 = new Arraylist<>(); arraylist3 = (ArrayList)getIntent().getSerializableExtra("fids"); for (i = 0; i – jai Feb 12 '20 at 07:45
  • problem #2 , then what do i do?? – jai Feb 12 '20 at 07:47
  • arraylist1 and arraylist3 both take in array of strings from firestore – jai Feb 12 '20 at 07:48
  • 1
    If you want to iterate through the values of `arrayList1`, use `arrayList1.size()` of course. – Stephen C Feb 12 '20 at 07:48
  • And please use **meaningful** variable names. – Stephen C Feb 12 '20 at 07:49
  • 1
    Or you could write `for (String s: arrayList1) { if (arrayList3.contains(s)) {count++;}}`. This is all pretty basic stuff ... Java 101 really. – Stephen C Feb 12 '20 at 07:53
  • how do i return the items that are matching? the elements stored in both the array lists's are string value's and they are queried from firestore – jai Feb 12 '20 at 08:54
  • Well ... I guess you just format them as text and put them in the text box. Your code is not "returning" anything. It is a `onClick` button. – Stephen C Feb 12 '20 at 09:14