从Java中2的ArrayList Corelating项目 [英] Corelating items from two arraylists in Java

查看:112
本文介绍了从Java中2的ArrayList Corelating项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我有一个包含类别名称微调框(下拉列表)。
用户通过它的名字中选择一个类别,说:食品和饮料

In my android app I have a spinner (drop-down) that contains category names. User selects a category by its name, say "Food and Drink"

这些分类都包含在一个ArrayList categoryNamesArray

those categories are contained in an ArrayList categoryNamesArray

还有另一种的ArrayList categoryIDsArray包含类别ID的整数。

there is another ArrayList categoryIDsArray which contains the category ID integers.

的顺序进行两场比赛,这意味着1类是汽车类,它应该是。

The ordering for both matches, meaning category 1 is category Automotive, which it is supposed to be.

我需要类别ID发送到数据库中,当用户按名称选择一个类别。我怎样才能做到这一点。

I need to send the category ID to the database, when the user chooses a category by name. How can I do that.

它得到与子分类比较复杂,因为它们的ID与40多岁开始。

It gets more complicated with Subcategories, because their IDs start with 40-something.

附加信息:

我拉从服务器的ID并把它们放在一个ArrayList,然后我拉类别名称从服务器,把他们在另一个数组列表。然后把两者的ArrayList在一个Object []对象,并将其传递给需要它们的活动

I pull the ids from the server and put them in an arraylist, then I pull the category names from the server and put them in another arraylist. Then put the two arraylists in one Object[] and pass them to the activity that needs them

推荐答案

如果你是,传统或其他原因,仅限于ArrayList中的数据结构,您可以通过执行获得相应的ID

If you are, for legacy or some other reason, restricted to the ArrayList data structure, you can get the corresponding id by doing

String chosenName; // user selects this
Integer id = categoryIDsArray.get(categoryNamesArray.indexOf(chosenName));

不过,这将是巨大的更好,如果你改变了code有两种

But, it will be hugely better if you change the code to have either


  1. 系统类别对象(通过Eluvatar的建议),或

  2. A BIMAP<整数,字符串方式> 这是一个双向的地图让你从一个名字来取得的ID,反之亦然

  1. A "Category" object (as suggested by Eluvatar), or
  2. A BiMap<Integer, String> which is a bidirectional map enabling you to get the id from a name, and vice versa.

这篇关于从Java中2的ArrayList Corelating项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆