0

I'm making my first android app using android studio. In this APP I have a listview with 12 classes(12 items). After clicking on one class, it goes into a tabbed activity with 10 items of this class. On each tab page I have a rating bar to let people rate the item.

I set an activity for the listview, and 12 independent activities for those 12 tabbed activities. The code from listview to each tabbed activity is like this:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if(i==0){
                Intent intent = new Intent(ListViewActivity.this, TabbedActivity1.class);
                intent.putExtra("styleName", STYLE_NAMES[i]);
                intent.putExtra("styleExample",STYLE_EXAMPLES[i]);
                startActivity(intent);
            }
            else if(i==1){
                Intent intent = new Intent(ListViewActivity.this, TabbedActivity2.class);
                intent.putExtra("styleName", STYLE_NAMES[i]);
                intent.putExtra("styleExample",STYLE_EXAMPLES[i]);
                startActivity(intent);
            }
    ...... // skip the other 10 tabbed activities. 
}

Now the problem is: after I finish rating on the tabbed activities, I return to the ListView activity and click into each tabbed activity again, the ratings are gone.

I guess the reason is that in my code, each time I click on the item it opens a new tabbed activity, although same layout, the contents are not saved.

So I was wondering whether I should do something on the ListView activity to save the ratings. I have searched for relevant questions, but I found in their scenarios, each list item is just a simple ratingbar. But here, my list item is a tabbed activity with 10 ratingbars.

Therefore, I have no idea how to do it. I have no experience in android studio, so I don't know where to start to solve the problem. Any idea is appreciated! Thanks a lot in advance!!

gladys0313
  • 2,569
  • 6
  • 27
  • 51

1 Answers1

0

First of all if all your tab activities are similar you can just create one activity instead of that many in your case 12 and pass the specific content and states via intent.

The basic approach to your question is would be store rating states in your main activity and when you open your tab activity each time you click the list items send the rate of the relevant activity with intent. Then in your tab activity update the rate with it.

To achieve this we are going to use startActivityForResult instead of startActivity because we need tab activity to return last state of rating bars.

You can see the basic example shown below here:

public class ListViewActivity extends AppCompatActivity {

    private static final int REQUEST_RATE = 1;
    private SparseIntArray rates = new SparseIntArray();

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ListViewActivity.this, TabActivity.class);
                intent.putExtra("styleName", STYLE_NAMES[i]);
                intent.putExtra("styleExample", STYLE_EXAMPLES[i]);
                intent.putExtra("position", i);
                intent.putExtra("rating", rates.get(i, 0));
                startActivityForResult(intent, REQUEST_RATE);
            }
        }

    }

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_RATE:
                if(resultCode == RESULT_OK) {
                    //retrieve and save rates
                    Bundle extras = data.getExtras();
                    int position = extras.getInt("position");
                    int rating = extras.getInt("rating");
                    rates.put(position, rating);
                }
                break;
        }
    }
}

public class TabActivity extends AppCompatActivity {

    private RatingBar ratingBar;
    private int position;

    @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        Bundle extras = getIntent().getExtras();
        position = extras.getInt("position");
        int rating = extras.getInt("rating");

        ratingBar.setRating(rating);
    }

    @Override protected void onDestroy() {
        //send current rating to list activity before we leave
        setResult();
        super.onDestroy();
    }

    private void setResult() {
        Intent intent = new Intent();
        intent.putExtra("position", position);
        intent.putExtra("rating", ratingBar.getRating());
        setResult(RESULT_OK, intent);
    }

}
Fatih Santalu
  • 4,641
  • 2
  • 17
  • 34
  • Thank you for your answer. But I don't know why it doesn't work in my case. Two problems: 1. I first keep my 12-activities-for-12-tabActivities setting and implement your rating retrieval code. However, the ``int rating = extras.getInt("rating")`` returns me ``0``. 2. I use 1 activity for those 12 activities and implement your codes, but the APP stops when I click on item on the ListView. I was wondering whether the problems are caused by my tab view design. In my case, each tab activity contains 10 views, each for a tab page and the ratingbars are distributed on each view. – gladys0313 Aug 13 '17 at 11:49
  • And therefore, 12 (tabbed activities) * 10 views = 120 views, so I have 120 ratingbars. Each time I return to a tabbed activity, 10 rating grades should be retrieved. Do you think this is the cause for the problem? – gladys0313 Aug 13 '17 at 11:50
  • The example assumes each tab activity has one rating. You can create a model that contains 10 ratings. Instead of `intent.putExtra("rating", ratingBar.getRating());` passing single rating here you can pass this model. And i pass the result before `onDestroy` you can try `onBackPressed` as well – Fatih Santalu Aug 13 '17 at 11:56
  • Thanks...I tried with 1 rating on tabbed activity first and use ``onBackPressed`` instead of ``onDestroy``. However, the APP stops from listview to tabbed activity if I have ``ratingBar.setRating(rating)`` in the tabbed activity. If I delete this line, it shows "The APP stops" when I want to return from tabbed activity to listview but afterwards the listview is shown and I can click on items, but the rating grade is (of course) not updated when I enter the tabbed activity again....Sorry for my no experience about it, but do you have any idea about it? – gladys0313 Aug 13 '17 at 12:53
  • you can see the crash log right? what is the exception do you get? – Fatih Santalu Aug 13 '17 at 12:54
  • btw you need to initialize rating bar before doing anything. In my example i just skip that part. I mean like `ratingBar = findViewById()` – Fatih Santalu Aug 13 '17 at 12:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151828/discussion-between-gladys0313-and-santalu). – gladys0313 Aug 13 '17 at 14:11