McColin1990 McColin1990 - 1 year ago 73
Android Question

Connecting different fragments with correct information

I'm novice in Android and few days ago started learning fragments. Kinda got stuck.

The whole idea is: Having two fragments. Fragment A. Fragment B. Both use Recyclerview+Items (Image+text).


I've working fragment A. I've partially working fragment B. I can go from fragment A -> fragment B by clicking any item (on frag. A), the problem is, fragment B shows the same information to every item on frag. A. How can I make sure, that moment, when I switch fragments it gives appropriate information (If I select drinks - I get vodka/martini ect..)
Information about fragment B is sitting at database, so if someone is up to giving a modul example, make sure to include example for using database as information source as well.

Answer Source

First modify the Category model adding the id

public class Category {
    private int id;

    public String getId() {
        return id;

    public void setId(int ind) { = id;

then set it when loading categories

        Category cat = new Category();

pass the id to fragment B as Rajesh suggested

            MealsListFragment fragment = new MealsListFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("categoryId", Categories.get(pos).getId());

get the value in fragment B and modify the getAllMealsValues to filter by the category id

    if (bundle != null) {
        //Load your data by id or any reference from your Model data

        RecyclerView rv = (RecyclerView) view.findViewById(;
        final Activity activity = getActivity();
        rv.setLayoutManager(new GridLayoutManager(activity, 2));
        rv.setAdapter(new MealsAdapter(this.getActivity(),  dbHelper.GetAllMealsValues(bundle.getInt("categoryId")) ));

        int mMargin = 15;
        rv.addItemDecoration(new SpacesItemDecoration(mMargin));

Change DBHelper to add the category id in meals table, just make sure to assign a category to the meal when adding meals

private static final String categoryId = "category_id";

private static final String CREATE_MEALS = "CREATE TABLE "
    + TABLE_Meals + "(" + KEY_ID + " INTEGER PRIMARY KEY," + mealsName
    + " TEXT unique," + desc + " TEXT," + size + " TEXT,"
    + price + " TEXT," + categoryId + " INTEGER)";

public Cursor getMeals(int id)
    Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM " + TABLE_Meals + " WHERE " + cagegoryId + "=" + String.valueOf(id), null);
    return cursor;


public ArrayList<Meals> GetAllMealsValues(int id)
    ArrayList<Meals> list = new ArrayList<Meals>();
    Cursor cursor =  getMeals(id);
    int i = 0; // Iterator for 'do-while'. While it gets Categories names, it shall catch all the url's from array as well.
    if (cursor.moveToFirst())
            Meals meal = new Meals();
            meal.setName(cursor.getString(cursor.getColumnIndex(mealsName))); //mealsName
        while (cursor.moveToNext());
    if (cursor != null && !cursor.isClosed())
    return list;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download