简体   繁体   English

Android-检查数组是否具有此值,获取位置并发送多余的意图到下一个活动片段

[英]Android - Check whether the array has this value , get the position and send intent put extra to next activity fragment

Refering to this question has been asked Android - Check whether a value exist in an Array 有人问过这个问题Android- 检查数组中是否存在值

So basically, I have 3 arrays in my FragmentActivity which are: 所以基本上,我的FragmentActivity中有3个数组:

    String[] productName = {"Bread", "Butter", "Cadburry", "Tin Spinach", "Bulla Ice Cream", "Kit Kat Jam", "Chili Sauce", "Jasmine Rice", "Nutela", "Logan Tin" };
    String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};
    int[] images = {R.drawable.i1, R.drawable.i2, R.drawable.i3, R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7, R.drawable.i8, R.drawable.i9, R.drawable.i10};

I want to get all postition in "expiryDate[]" which contains value = "27/September/2017". 我想在“ expiryDate []”中获取所有职位,其中包含值=“ 27 / September / 2017”。

After getting the postion of the element in expiryDate[], I want to use the postion obtained from expiryDate[] for productName[] and images[] as well, to get the value based on the same pasition in expiryDate[]. 在expiryDate []中获得元素的位置之后,我想对productName []和images []也使用从expiryDate []获得的位置,以基于expiryDate []中的相同位置获得值。

At the end I want to intent the values obtained from productName[], images[], and expiryDate[] to another FragmentActivity and put them into new arrays. 最后,我想将从productName [],images []和expiryDate []获得的值用于另一个FragmentActivity,并将它们放入新的数组中。

Can anybody please tell me how to code this scenario ? 有人可以告诉我如何编写此方案吗? I really appreciate it. 我真的很感激。

Try this ..... 尝试这个 .....

int position=-1;

position= Arrays.asList(array).indexOf("27/September/2017");
String name , image;
if(position>=0){
name = productName [position];
image = images [position];
}

EDIT:- please have look at this ..... 编辑:-请看看这个.....

>  for(int i=0;i<expiryDate.lenght;i++){
>     if(expiryDate[i].equals("27/September/2017"))
>     {
>         // do opeartion here 
          // add all values in new arrays
          // send all new arrays to the next activity.
>     }
>     }

OR 要么

    ArrayList<Int> positions =  new ArrayList<>();
    >  for(int i=0;i<expiryDate.lenght;i++){
    >     if(expiryDate[i].equals("27/September/2017"))
    >     {
    >        positions.add(i);
             // send all your existing array list with this positions array list to other activity
    >     }
    >     }

Hope it will help ... 希望对您有所帮助...

Getting the images and product names: 获取图像和产品名称:

ArrayList<String> expiredProductNames = new ArrayList<>();
ArrayList<Integer> expiredProductImages = new ArrayList<>();

String expiredDate = "27/September/2017";

for (int i = 0; i < expiryDate.length; i++) {
    if (expiryDate[i].equals(expiredDate)) {
        expiredProductNames.add(productName[i]);
        expiredProductImages.add(images[i]);
    }
}

Creating intent: 创建意图:

Intent intent = new Intent(FragmentActivityA.this, FragmentActivityB.class);
intent.putStringArrayListExtra("productNames", expiredProductNames);
intent.putIntegerArrayListExtra("images", expiredProductImages);

Retrieving values from intent in second FragmentActivity: 在第二个FragmentActivity中从意图中检索值:

ArrayList<String> productNames;
ArrayList<Integer> images;

Intent intent = getIntent();
if (null != intent) {
    productNames = intent.getStringArrayListExtra("productNames");
    images = intent.getIntegerArrayListExtra("images");
}

Note: It is better to use a class to encapsulate the data so there is only one list to iterate and propagate like other suggestions here. 注意:最好使用一个类来封装数据,以便像这里的其他建议一样,只有一个列表可以迭代和传播。

To get the positions of the item in the array follow the below code 要获取项目在数组中的位置,请遵循以下代码

  String[] expiryDate = {"27/September/2017", "30/September/2017", "30/October/2017", "28/September/2018", "2/January/2018", "29/December/2017", "27/September/2017", "28/September/2017", "13/February/2018", "20/December/2017"};

    private List<Integer> postition = new ArrayList<>();

    for (int i = 0; i < expiryDate.length; i++) {
                if (expiryDate[i].equals("27/September/2017")) {
                    postition.add(i);
                }

      }
 Log.i("array positions", postition.toString());

With this given expireDate array you get the values 0 and 6 in the positions array. 使用给定的expireDate数组,您可以在positions数组中获得值0和6。 Now to get the values from productName and images use the below code 现在要从productName和图像中获取值,请使用以下代码

private List<String> products = new ArrayList<>();
for (int i = 0; i < postition.size(); i++) {
            products.add(productName[postition.get(i)]);
        }
Log.e("products", products.toString());

Now you get values Bread and Chilli Sauce for the existing productName array. 现在,您将获得现有productName数组的值Bread和Chilli Sauce。 Do the same for images as well. 对图像也是如此。

Hope this helps. 希望这可以帮助。

If those three arrays are related, you should really leverage encapsulation to maintain the relationship between the data. 如果这三个数组相关,则应该真正利用封装来维护数据之间的关系。 It would be easier to maintain and read. 维护和阅读起来会更容易。 It should allow you to mutate the data without having to worry about keeping 3 arrays in sync (to save you from hitting that inevitable index out of bounds problem) 它应该允许您更改数据,而不必担心保持3个数组同步(以免您遇到不可避免的索引越界问题)

class Product {

    // you probably want getters/setters for these
    public String name;
    public String expiration;
    public int drawableId;

    public Product(String name, String expiration, int drawableId){
        this.name = name;
        this.expiration = expiration;
        this.drawableId = drawableId;
    }
}

After you do that, it's much easier to filter one large list: 完成此操作后,过滤一个大列表会容易得多:

List<Product> myProducts = ...;
List<Product> filteredProducts = new ArrayList<>();

for (Product product : myProducts){
    if (product.expiration.equals("27/September/2017")){
        filteredProducts.add(product);
    }
}
// by now, filteredProducts should contain all the items you wanted

If you have access to rxjava, java8, or guava, you can make it a little cleaner: 如果您可以访问rxjava,java8或guava,则可以使其更加简洁:

rxjava rxjava

List<Product> myProducts = ...;
List<Product> filteredProducts = Observable.fromIterable(myProducts)
    .filter(product -> product.expiration.equals("27/September/2017"))
    .toList()
    .blockingGet();
// by now, filteredProducts should contain all the items you wanted

java 8 Java 8

List<Product> myProducts = ...;
List<Product> filteredProducts = myProducts.stream()
    .filter(product -> product.expiration.equals("27/September/2017"))
    .collect(Collectors.toList());
// by now, filteredProducts should contain all the items you wanted

guava 番石榴

List<Product> myProducts = ...;
List<Product> filteredProducts = Collections2.filter(
        myProducts,
        (product) -> product.expiration.equals("27/September/2017"));
// by now, filteredProducts should contain all the items you wanted

So, if you leverage encapsulation, now all you need to do to get your product names are: 因此,如果您利用封装,那么现在要做的就是获取产品名称:

for(Product product : filteredProducts){
    String productName = product.name;
    // do whatever you wanted with this data
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM