0

I have RecyclerViewerActivity with custom adapter inside it. There are product categories in the RecyclerViewer and the adapter holds name for each category like:

category 1 = "drinks", category 2 - "dairy", category 3 = "pasta".

When user clicks on "drinks" - new ActivityDrinks called with list view to add items for this category- he can add soda, cola.

If he clicks on "dairy" - ActivityDairy opens and he can add milk, yogurt...

Now I try to implement some indicator of existing items in the list and pass it to the adapter.So if there is at least one item in ActivityDrinks - I want to display some image above the name of the category in my adapter (in RecyclerViewer activity). So the user could see if he has some items in category or no.

I added boolean variable for checking ArrayList in every activity (ActivityDrinks, ActivityDairy):

if (arrayList == null) {
    return false;
}
else {
    return true;
}

This part works, but now I want to display image above the the name of the category in my adapter (adapter holds name of each category + ImageView to hold indicator) depending of value of the variable: if variable = true - display image.

This is my Layout code (for single card of RecyclerView):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:id="@+id/ImageBack"
       android:background="@drawable/back_standart">

       <!--name of Category (like "drinks")-->
       <TextView
        android:text="text"
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="22dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="35dp"
        android:layout_marginRight="8dp" />

      <!--desired indicator-->
       <ImageView
        android:background="@drawable/iclun"
        android:id="@+id/badgeView"
        android:layout_marginLeft="100dp"
        android:textColor="#FFF"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/item_title" />

 </RelativeLayout>

enter image description here

And this is my code for checking Array list in ActivityDrinks (Using Firebase):

      public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snap: dataSnapshot.getChildren()){
                Log.e(snap.getKey(),snap.getChildrenCount()+"");
            }
            if(dataSnapshot.getChildrenCount()>0){
                index_badge = true;

My Adapter:

      @Override
      public void onBindViewHolder(ViewHolder viewHolder, final int position) {

     viewHolder.txtViewTitle.setText(itemsData[position].getItem_title());                   

     viewHolder.badge.setImageResource(itemsData[position].getBadge_index());

I tried to use putExtras and pass the value of my variable to RecyclerViewActivity. But I don't really understand how to pass the value of variable to each item in RecyclerView. It is possible that for "Drinks" it will be true and for "Diary" - false because there are no items in the ActivityDairy. In this case in my adapter for Drinks I need to display the image/ indicator and for Diary - not.

Sufian
  • 6,405
  • 16
  • 66
  • 120
user5866501
  • 483
  • 2
  • 5
  • 12
  • 2
    add your code and screenshot of desired layout – USKMobility Jun 11 '16 at 07:51
  • Yes, please add some code (not all!) and screenshot(s). As it stands, your question is wordy and not easy to answer because you we don't know what the actual problem is. – Sufian Jun 11 '16 at 07:58
  • please see edited question – user5866501 Jun 11 '16 at 08:13
  • If I understand correctly, you want to `startActivityForResult` and receive data in `onActivityResult`. You are welcome to find your own tutorials on those. – OneCricketeer Jun 11 '16 at 08:21
  • I tried to use putExtras and pass the value of my variable to RecyclerViewActivity. But I don't really understand how to pass the value of variable to each item in RecyclerView. It is possible that for "Drinks" it will be true and for "Diary" - false because there are no items in the ActivityDairy. In this case in my adapter for Drinks I need to display the image/ indicator and for Diary - not – user5866501 Jun 11 '16 at 08:28
  • You can pass an ArrayList of `Parcelable` objects through putExtras – OneCricketeer Jun 11 '16 at 08:30
  • Can you explain a little bit more please? – user5866501 Jun 11 '16 at 08:35
  • @user5866501 the code you've posted is not relevant to the question you have asked. You're asking how to pass some information (string and image) from ActivityA to ActivityB, but are missing the Java code where you start ActivityB. Posting XML is only adding noise here. Please keep you question as simple as possible to get quick response. – Sufian Jun 11 '16 at 11:15
  • Possible duplicate of [Pass a String from one Activity to another Activity in Android](http://stackoverflow.com/questions/6707900/pass-a-string-from-one-activity-to-another-activity-in-android) – Sufian Jun 11 '16 at 13:22

2 Answers2

1

Intent should be used in passing values from one activity to another.

You can have something like this:

List<DAIRY> listOfDairy = new ArrayList<DAIRY>();
// add dairy...

Intent intent = new Intent(this, ReceivingClass.class);
i.putExtra("KEY_DAIRY", listOfDairy);
startActivity(intent); 

Then on your ReceivingClass, you can retrieve it like this one:

List<DAIRY> dairyList = getIntent().getParcelableArrayListExtra("KEY_DAIRY");

If you do not need a list, then you can just pass a String/Boolean via Intent. To do this:

Intent intent = new Intent(this, ReceivingClass.class);    
i.putExtra("KEY_HAS_DAIRY", true);
startActivity(intent);

Then

boolean hasDairy = getIntent().getBooleanExtra("KEY_HAS_DAIRY");
shibapoo
  • 1,909
  • 3
  • 16
  • 22
-1

Solved the problem by using SharedPreferences. I send value from ActivityDrinks to RecyclerViewerActivity:

    final SharedPreferences pref = getSharedPreferences("filename",MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean("key",index_badge);
    editor.apply();
    Log.e("key","index"+index_badge);

And retrieve the value in RecyclerViewerActivity:

     SharedPreferences pref = getSharedPreferences("filename", MODE_PRIVATE);
     Boolean a = pref.getBoolean("key", false);

The code works for me.

user5866501
  • 483
  • 2
  • 5
  • 12
  • To pass data from one Activity to another, you should use `Intent`. Usage of `SharedPreference` or SQLite to achieve it is plain wrong, although it may allow you to keep things working for now. – Sufian Jun 11 '16 at 13:03
  • `SharedPreference` is not designed for this, so it can come to haunt you in future. Always use `Intent`s for passing data between activities. – Sufian Jun 11 '16 at 13:12
  • 1
    Instead of waiting to post a full answer later, post that answer now. Without that this answer is not very useful. – Frank van Puffelen Jun 11 '16 at 15:11