0

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).

Idea: https://img.exs.lv/l/a/lat-deels/frags.png

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.

  • Could you add the code for GetAllMealsValues method, the table structure sounds strange. – diedu Nov 24 '16 at 03:10
  • @diedu I added DBHelper. See my edited post. Also... That's just how I imagine table structure. Right now GetAllMealsValues just gets all meals (12 for testing). I thought it would be easier if I implement db as I said previously (haven't done it yet) –  Nov 24 '16 at 10:32

2 Answers2

0

//IN YOUR ADAPTER

//Create instance of your fragment and pass value with bundle 
MealsListFragment fragment = new MealsListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("object", value);//Model class or you can pass putStringExtra
        fragmentGivenTipDetail.setArguments(bundle);


 FragmentManager fragmentManager = c.getFragmentManager();
        FragmentTransaction fragmentTransaction =     fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.root_layout, mealsListFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

IN FRAGMENT MealsListFragment in onCreateView

Bundle bundle=getArguments();
if (bundle != null) {
     Model model = (Model) bundle.getSerializable("object");
           or
     String id=bundle.getStringExtra("id");
     //Load your data by id or any reference from your Model data
}

dbHelper.GetAllMealsValues() instead of this filter by id of parent

Rajesh
  • 2,618
  • 19
  • 25
  • 1. Can you take a look at my database and little bit change your code so I can clarify how I can pass in the correct data? I'm not sure how to get data by id - as I've alot of data (will have) maybe it's easier to collect it by columns - as you can see I did that in my DBHelper class. Right now I get error at "value" parameter when I serialize and not sure what "Model" object is. It was meant to be my DBHelper class object? –  Nov 24 '16 at 11:14
  • you can use search query for find data by it's id, have look of search query public Model getItemById(String itemId) { String countQuery = "SELECT * FROM " + TABLE + " WHERE " + TABLE_KEY_ID + "=" + itemId"; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); ...create model/list and return it } – Rajesh Nov 24 '16 at 11:30
  • Can you please edite your answer and put the code there - impossible to read, haha. Also, what is meant by "MODEL" ArrayList? –  Nov 24 '16 at 11:54
  • sorry no time for snippet your code, yes model is POJO your gatter setter class but it should be serializable to pass in bundle – Rajesh Nov 24 '16 at 11:55
  • I edited code. At the bottom I tried to write the code you suggested. Can you take a look? I'm getting error at 'bundle.putSerializable' value parameter. Not sure what to put there. –  Nov 24 '16 at 12:28
0

First modify the Category model adding the id

public class Category {
    private int id;
    ...

    public String getId() {
        return id;
    }

    public void setId(int ind) {
        this.id = id;
    }
    ...
}

then set it when loading categories

        Category cat = new Category();
        cat.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)))
        cat.setName(cursor.getString(cursor.getColumnIndex(catName)));
        cat.setImage(category_image_urls[i]);
        list.add(cat);

pass the id to fragment B as Rajesh suggested

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

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(R.id.recycler_view);
        final Activity activity = getActivity();
        rv.setLayoutManager(new GridLayoutManager(activity, 2));
        rv.setAdapter(new MealsAdapter(this.getActivity(),  dbHelper.GetAllMealsValues(bundle.getInt("categoryId")) ));
        //ADDING SOME SPACE AROUND FRAGMENTS

        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())
    {
        do
        {
            Meals meal = new Meals();
            meal.setName(cursor.getString(cursor.getColumnIndex(mealsName))); //mealsName
            list.add(meal);
            i++;
        }
        while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed())
    {
        cursor.close();
    }
    return list;
}
diedu
  • 19,277
  • 4
  • 32
  • 49
  • Thank you. Alot of things got clearer. Except two things: 1) So I've to make a int id parameter @addMeals function? 2) I implemented your suggestions. Now when I click any category I get blank white screen - it doesn't show recycler. –  Nov 24 '16 at 23:07
  • yes, wherever you are adding your meals data you have to assign a category id, in order to do so you have to change addMeals method – diedu Nov 24 '16 at 23:12
  • diedu Alright. Any ideas why I lost my recycler view, when I click on any cat name? –  Nov 24 '16 at 23:15
  • I guess it is because there is not data to show, make sure you are getting the meals data from the database for the selected category first. – diedu Nov 24 '16 at 23:18
  • Checked but didn't see why it doesn't get the meals. I edited post [topic]. Can you take a look? –  Nov 25 '16 at 08:24
  • @McColin1990 could you check the data in your sqlite database http://stackoverflow.com/a/29206366/1868008 to see if meals records have a value for the category_id field – diedu Nov 26 '16 at 01:33