2

A little context: I have a stable tutorials app on the market. Recently the users have been requesting some external sources where they can learn more. This question is regarding the solution I came up with.

I have an activity with a listview and a spinner on top.

The listview can be populated with up to 25 elements at once (should I make it lesser?) .... I want the spinner to have options like (1-25, 26-50, 51-75).

The default can be set to 1-25. Depending on what the user selects, I need to re-populate the listview accordingly. I reckon a two dimensional array would be the neatest. But I don't want to hinder the performance too.

Anyways, here's what I've done yet. I need to know how exactly should I update the listview elements in the Spinners OnClick.

  Spinner spinner;
    String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
    String Laptops[] = {"Dell","Hp","Apple"};
    ListView lstView;


            lstView = (ListView) findViewById(R.id.listView1);

            ArrayAdapter<String> adapter = new ArrayAdapter<String> (Category.this, android.R.layout.simple_spinner_item, path );
            spinner = (Spinner) findViewById(R.id.spinner1);
            spinner.setAdapter(adapter);
            spinner.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
                    lstView.setAdapter(lstAdapter);
                    //lstView.refreshDrawableState();

                }
            });

Assume that I have to switch between the two arrays depending on the spinner selection.

Thank you for reading! If you require any more information, kindly ask :-)

  • 1
    ListViews are made for showing lists of data, which you are trying to deconstruct by adding a spinner to switch between subsections of the list data. Why can't you just add everything to the ListView, then use the spinner if you must to scroll to a particular position in the ListView? – Ken Herbert Dec 13 '13 at 04:06
  • My listview is probably going to contain many (200+) items... With performance in mind, I think initializing the entire thing at once would cause lags like nobody has ever seen. – user3097960 Dec 13 '13 at 04:09
  • I agree with winterblood, I think a better approach would be to to use row recycling on your list view so that the number of items displayed isn't an issue and then if necessary use a spinner or another navigation method to move down the list. Consider how other applications handle long lists (Gmail for example), mainly they make use of either infinite lists, pages (similar to your suggestion), or filters. – TheIT Dec 13 '13 at 04:12
  • android only populate the views of listview when it is visible. For example your list contains 1000 items but at a time only 8 are visible then android will only populate 8 list items. If you scroll down more are populated and above may be reused. So there is no performance hit. – Rohan Kandwal Dec 13 '13 at 04:12
  • It appears as though you may not be aware of row recycling for list views. Take a look a this question (http://stackoverflow.com/questions/19289812/findviewbyid-vs-view-holder-pattern-in-listview-adapter) or look up row recycling or the holder pattern. – TheIT Dec 13 '13 at 04:15
  • @RohanKandwal There can be a performance hit in scrolling a ListView, but it largely depends on what is being shown in the ListView item, and how you optimize the code behind it. – Ken Herbert Dec 13 '13 at 04:15
  • Thank you for the suggestions! I will look into row recycling. – user3097960 Dec 13 '13 at 04:21
  • @winterblood yes but seeing the code above i see no reason for a performance hit. – Rohan Kandwal Dec 13 '13 at 04:22
  • 1
    @RohanKandwal It will mostly depend on what the adapter's `getView()` method is doing. If there are images being loaded then it may become a bottleneck. – Ken Herbert Dec 13 '13 at 04:25
  • @winterblood yes i understood what you `it largely depends on what is being shown in the ListView item` but the user here is just using String. – Rohan Kandwal Dec 13 '13 at 04:27
  • @RohanKandwal Good point, sorry to push when I had taken only a brief look at the code. – Ken Herbert Dec 13 '13 at 04:29
  • @winterblood No problems you were just trying to help :D – Rohan Kandwal Dec 13 '13 at 04:31

0 Answers0