您如何使用Parcelable传递arraylist索引中的元素?

问题描述 投票:-1回答:1

在比赛中,我需要将数组索引的所有元素传递给另一个活动。这就是我所拥有的...

    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");
               }
           }

if语句比较locationReview数组中的id元素。如果该元素与文本视图中的字符串匹配,则我需要i的数组索引,并将其所有元素添加为可折叠的数组,并作为Extra添加到我的意图中。

java android arrays parcelable
1个回答
0
投票

我将解决方案分为几个步骤:

步骤1。确保ArrayList模型类实现了Parcelable接口。

Step 2。遍历locationReview数组的所有元素,以找出其索引符合您的要求。您可以在类中定义另一个“过滤的” ArrayList对象,并用您的(locationReview.get(i).id == Integer.parseInt(tvId.getText()。toString())]过滤的项目填充该对象。 >条件。

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))
        }
    }

步骤3。

超出循环范围,将经过过滤的Parcelable数组通过
LocationReview.putParcelableArrayListExtra("locationReview", filteredlocationReview);

步骤4

在需要使用getParcelableArrayListExtra方法来获取数组的地方开始新的活动。
© www.soinside.com 2019 - 2024. All rights reserved.