0

On a match I need all the elements of an array index passed to another activity. Here's what I have...

    Intent LocationReview = new Intent(v.getContext(), ReviewActivity.class);
     // iterate through array 
    for (int i = 0; i < locationReview.size(); i++) {

int locationID = locationReview.get(i).id;
int currentID = Integer.parseInt(tvId.getText().toString());

          // compare id no. at index i of array
          if ((locationReview.get(i).id == Integer.parseInt(tvId.getText().toString()))) {

          // if match set putExtra array to locationReview.get(i)
              **LocationReview.putParcelableArrayListExtra("locationReview", locationReview.get(i));**
              itemView.getContext().startActivity(LocationReview);
              } else {
                Log.e("VerticalAdapter", "no matcon on locationReview");
               }
           }

The if statment compares the id element in the locationReview array. If the element matches the string in the text view I need array index of i , and all its elements, added as a pracelable array and added as Extra to my intent.

c1scok1d
  • 57
  • 1
  • 9

1 Answers1

0

I would split the solution into several steps:

Step 1. Make sure your ArrayList model class implements the Parcelable interface.

Step 2. Iterate through all elements of locationReview array to figure out which its indexes are matching your requirements. You may define another "filtered" ArrayList object in the class and populate it with the items filtered by your (locationReview.get(i).id == Integer.parseInt(tvId.getText().toString())) condition.

ArrayList<Object> filteredlocationReview = new ArrayList<>();
    for (int i = 0; i < locationReview.size(); i++) {

        int locationID = locationReview.get(i).id;
        int currentID = Integer.parseInt(tvId.getText().toString());

        // compare id no. at index i of array
        if ((locationReview.get(i).id == Integer.parseInt(tvId.getText().toString()))) {
            filteredlocationReview.add(locationReview.get(i))
        }
    }

Step 3. Being out of the loop scope put the filtered Parcelable array through

LocationReview.putParcelableArrayListExtra("locationReview", filteredlocationReview);

Step 4. Start new activity where you need to make use of getParcelableArrayListExtra method to get your array.

Andrii Artamonov
  • 622
  • 8
  • 15