【问题标题】:How do you pass elements from an arraylist index using Parcelable?如何使用 Parcelable 从 arraylist 索引传递元素?
【发布时间】:2019-11-25 20:21:19
【问题描述】:

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

    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】:

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

    第 1 步。确保您的 ArrayList 模型类实现 Parcelable 接口。

    第 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 方法获取数组的地方开始新活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 2012-06-12
      • 2019-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多