Skip to content Skip to sidebar Skip to footer

How To Get Items From A String-array Using An Id-based Mechanism?

The problem is simple. I've implemented a Navigation Drawer in Android. Now, I have a menu, where each item is an item of a string-array in strings.xml. A fragment is created every

Solution 1:

As suggested by Selvin, I've created a JSON file in assets/ in which each element of the JSON array corresponds to a menu item including three fields (id, name and icon), e.g.:

[{"id":1,"name":"item1","icon":"icon1"},{"id":2,"name":"item2","icon":"icon2"},{"id":3,"name":"item3","icon":"icon3"},{"id":4,"name":"item4","icon":"icon4"}]

Then, I firstly created an Item class to map the JSON object:

publicclassItem {
    private int id;
    privateString name;
    privateString icon;

    publicItem(JSONObjectobject) {
        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;
    }

    publicvoidsetId(int id) {
        this.id = id;
    }

    publicStringgetName() {
        return name;
    }

    publicvoidsetName(String name) {
        this.name = name;
    }

    publicStringgetIcon() {
        return icon;
    }

    publicvoidsetIcon(String icon) {
        this.icon = icon;
    }

    // Factory method to convert an array of JSON objects into a list of objects// User.fromJson(jsonArray);publicstaticArrayList<Item> fromJson(JSONArray jsonObjects) {
    ArrayList<Item> items = newArrayList<Item>();
    for (int i = 0; i < jsonObjects.length(); i++) {
        try {
            items.add(newItem(jsonObjects.getJSONObject(i)));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return items;
    }

}

and the corresponding adapter:

publicclassItemAdapterextendsArrayAdapter<Item> {

    publicItemAdapter(Context context, ArrayList<Item> items) {
        super(context, 0, items);
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {

        // Get the data item for this positionItemitem= getItem(position);
        intitem_id= item.getId();
        Stringitem_name= item.getName();
        intitem_icon_id= parent.getContext().getResources().getIdentifier(item.getIcon(),
                "drawable", parent.getContext().getPackageName());

        // Check if an existing view is being reused, otherwise inflate the viewif (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.drawer_list_item, parent, false);
        }

        // Lookup view for data populationImageViewitem_icon_view= (ImageView) convertView.findViewById(R.id.item_icon);
        TextViewitem_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 screenreturn convertView;
    }
}

Finally in my activity I pass the id of the selected item:

Fragmentfragment=newMainFragment();
Bundleargs=newBundle();
args.putInt(MainFragment.ARG_ITEM_ID, item.getId());
args.putString(MainFragment.ARG_ITEM_NAME, item.getName());
fragment.setArguments(args);

and I switch in my fragment as in the following:

int item_id = getArguments().getInt(ARG_ITEM_ID);

switch(item_id) {
    case1:
        [...]

    case2:
        [...]

[...]

Hope my example can help other people.

Post a Comment for "How To Get Items From A String-array Using An Id-based Mechanism?"