0

I've implemented Sherlock action bar tabs today, so my tab handling class extends SherlockActivity implements ActionBar.TabListener.

I start some empty layout, and then each tab has it's own layout, and it works fine.

First, on my first tab I need a list (but can't extend listview obviously). I'm using Strings and then string array

<string-array name="my_keys">
    <item>@string/mytab_mymonitor</item>
    <item>@string/mytab_mymessaging</item>
    <item>@string/mytab_information</item>
</string-array>

So first, how to populate listview from strings using adapter?

I tried this, but it crashes my app:

myKeys = getResources().getStringArray(R.array.my_keys);
        ListView mListView = (ListView) findViewById(R.id.lvMyList);

        mListView.setAdapter(new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, myKeys));

Second question here is, as I'll have 5 tabs with lot of data processing, is it normal to have everything within "Tab.Listener" activity, or could I somehow use multiple classes / activities while my tabs would still be on place?

Balkyto
  • 1,460
  • 4
  • 22
  • 47
  • "it crashes my app" -- examine LogCat and look at the stack trace associated with your crash. "Is it normal to have everything within "Tab.Listener" activity" -- `Tab.Listener` is an interface, not an `Activity`. – CommonsWare Sep 12 '12 at 13:01
  • That's why I placed it under quotations, and that's why I'm asking if I can implement multiple activities for each tab. Sorry that I'm stupid, I'm born this way :) – Balkyto Sep 12 '12 at 13:36

1 Answers1

1

The key is to use Tabs with a ViewPager and Fragments. That way you could put any type of content you need (e.g. you could use a ListFragment).

Check out this code snippet I've written in another answer.

Community
  • 1
  • 1
Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • Hey, I've red the snippet, but now I have problems with ViewPager. Is that part of Sherlock, or in separate library that I should use? Beside that, code that you gave is giving me "FragmentA" (and all others) can't be resolved to a type. I'm obviously missing something, again... – Balkyto Sep 12 '12 at 13:39
  • ViewPager is not part of ActionBarSherlock. You have to use the [compatibility library](http://developer.android.com/tools/extras/support-library.html). FragmentA is just an example. You have to create your own fragments. – Benito Bertoli Sep 12 '12 at 14:18
  • Yep, I've figured out about ViewPager, it was my Eclipse. One nice restart of Eclipse and suddenly he was familiar with ViewPager... Create fragments? I must admit I've never used them at all, so I should create Classes "public class FragmentA extends Fragment" ... and then import them to that Main activity? – Balkyto Sep 12 '12 at 14:24
  • Yes, but you have to extend `SherlockFragment` in this case. – Benito Bertoli Sep 12 '12 at 14:54
  • Ok, I have my 5 empty SherlockFragments - I assume I should have onCreate or OnCreateView and inflated content for each activity? I have MainActivity that extends SherlockFragmentActivity, and code set as in your example (with my fragments), I still have no clue what is pager element on main.xml, but it is there. Is there anything else that I need now? :) – Balkyto Sep 12 '12 at 15:52
  • Ok, Update :) I got Tabs to work in Fragments! Tnx!! Now I have different problem, that is I have no clue how to inflate each tab from it's fragment because it is different than activity, but it is "off subject" – Balkyto Sep 12 '12 at 16:22
  • I've just figure out that code written like this has a Bug, as pager is not following tabs, so I can have multiple pages within a Tab and slide them. – Balkyto Sep 13 '12 at 17:29
  • Yes, in fact there are a couple of lines missing. @Override public void onPageSelected(int position) { getSupportActionBar().setSelectedNavigationItem(position); } – Benito Bertoli Sep 14 '12 at 06:38