Android 如何使用基于ID的机制从字符串数组中获取项?
问题很简单。我在Android中实现了一个导航抽屉。现在,我有一个菜单,其中每个项都是Android 如何使用基于ID的机制从字符串数组中获取项?,android,arrays,xml,string,android-fragments,Android,Arrays,Xml,String,Android Fragments,问题很简单。我在Android中实现了一个导航抽屉。现在,我有一个菜单,其中每个项都是strings.xml中string数组的一项。每次用户单击其中一个菜单项时,都会创建一个片段。在fragment类中,我希望在项目之间切换,以便用适当的信息填充主要内容。我知道的唯一方法是检查项目的位置,但是如果将来我再添加一个项目,会发生什么?我需要更改代码中的所有位置值 因此,我想知道是否有一种方法可以为每个元素分配一种ID。我已经读过为它们中的每一个定义一个单独的字符串,但是如何检查这些值呢?我尝试了以
strings.xml
中string数组的一项。每次用户单击其中一个菜单项时,都会创建一个片段。在fragment类中,我希望在项目之间切换
,以便用适当的信息填充主要内容。我知道的唯一方法是检查项目的位置,但是如果将来我再添加一个项目,会发生什么?我需要更改代码中的所有位置值
因此,我想知道是否有一种方法可以为每个元素分配一种ID。我已经读过为它们中的每一个定义一个单独的字符串,但是如何检查这些值呢?我尝试了以下方法,但它给了我一个常量表达式required
错误:
Resources resources = getResources();
switch(item_string) {
case resources.getString(R.string.item1):
//TODO
case resources.getString(R.string.item2):
//TODO
...
正如Selvin所建议的,我在
assets/
中创建了一个JSON文件,其中JSON数组的每个元素对应一个菜单项,包括三个字段(id、名称和图标),例如:
然后,我首先创建了一个Item
类来映射JSON对象:
public class Item {
private int id;
private String name;
private String icon;
public Item(JSONObject object) {
try {
this.id = object.getInt("id");
this.name = object.getString("name");
this.icon = object.getString("icon");
} catch (JSONException e) {
e.printStackTrace();
}
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
// Factory method to convert an array of JSON objects into a list of objects
// User.fromJson(jsonArray);
public static ArrayList<Item> fromJson(JSONArray jsonObjects) {
ArrayList<Item> items = new ArrayList<Item>();
for (int i = 0; i < jsonObjects.length(); i++) {
try {
items.add(new Item(jsonObjects.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
return items;
}
}
我在片段中切换如下:
int item_id = getArguments().getInt(ARG_ITEM_ID);
switch(item_id) {
case 1:
[...]
case 2:
[...]
[...]
希望我的示例可以帮助其他人。对于字符串数组,应该使用索引。对于单独的字符串,可以使用反射。对于数据库表。。。我想你知道怎么管理它。哦,来吧。。。在原始/资产中使用data.json。。。这会帮你省去很多麻烦。。。您需要简单数组:使用字符串数组。。。您需要一些结构化数据使用:xml/json/database可能是您可以为每个项设置标记(从抽屉列表适配器的getTag方法)并在此处使用。或者定义另一个与现有数组长度相同的数组,并定义此新数组中的所有id。使用现有数组中的导航项位置从此新数组中获取id值。请确保这两个阵列保持同步。@Selvin:请您更准确一点好吗?
public class ItemAdapter extends ArrayAdapter<Item> {
public ItemAdapter(Context context, ArrayList<Item> items) {
super(context, 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
int item_id = item.getId();
String item_name = item.getName();
int item_icon_id = parent.getContext().getResources().getIdentifier(item.getIcon(),
"drawable", parent.getContext().getPackageName());
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_list_item, parent, false);
}
// Lookup view for data population
ImageView item_icon_view = (ImageView) convertView.findViewById(R.id.item_icon);
TextView item_name_view = (TextView) convertView.findViewById(R.id.item_name);
// Populate the data into the template view using the data object
item_name_view.setText(item_name);
item_icon_view.setImageResource(item_icon_id);
// Return the completed view to render on screen
return convertView;
}
}
Fragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putInt(MainFragment.ARG_ITEM_ID, item.getId());
args.putString(MainFragment.ARG_ITEM_NAME, item.getName());
fragment.setArguments(args);
int item_id = getArguments().getInt(ARG_ITEM_ID);
switch(item_id) {
case 1:
[...]
case 2:
[...]
[...]