0

I have a ListView with ViewSwitcher items. The goal is to tap a list item to switch between ViewSwitcher views. Tapping works as expected, but scrolling away from a tapped item and then back sometimes changes which item is displaying the second view from the ViewSwitcher.

Here is my ListView item layout, the ViewSwitcher is @+id/details:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    android:layout_width  = "fill_parent"
    android:layout_height = "wrap_content" >

    <ViewSwitcher
        android:id                 = "@+id/details"
        android:layout_width       = "fill_parent"
        android:layout_height      = "fill_parent"
        android:measureAllChildren = "false" >

        <!-- two views I want to switch between are here -->

    </ViewSwitcher>
</RelativeLayout>

My Activity that finds @id/details and calls .showNext().

public class MyActivity extends ListActivity {
    private MySQLiteOpenHelper data;
    private SimpleCursorAdapter adapter;
    private Cursor cursor;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] FROM = { "_id", "_id", "name", "name" };   
        int[] TO = { R.id.my_list_id, R.id.my_list_id2, R.id.my_list_name, R.id.my_list_name_details };

        data = new MySQLiteOpenHelper(this);
        cursor = data.myList();
        startManagingCursor(cursor);

        adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, FROM, TO);
        setListAdapter(adapter);

        ListView list = getListView();
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
                ViewSwitcher item = (ViewSwitcher) view.findViewById(R.id.details);
                item.showNext();
            }
        });
    }
}

I think my problem might be that @id/details is not actually unique, so undefined behavior happens when scrolling.

If this is the case, then how can I assign a unique ID to each ViewSwitcher or find the correct ViewSwitcher to call .showNext() on?

mjwhitt
  • 63
  • 4
  • when you scroll back to a switched ViewSwitcher? You find it unSwitched? – Sherif elKhatib Aug 09 '11 at 23:56
  • Yes, when I scroll back it is unswitched and a *different* item is switched. – mjwhitt Aug 10 '11 at 00:01
  • Example: 10 items in list, tap #9, scroll up, tap #2 (can't see #9), scroll down and #9 is unswiched and #10 *is* switched. Scroll up and tap #2 again, scroll down, and #9 is backed to being switched. Strange unswitching/switching only seems to happen to ViewSwitchers outside of view in the ListView. – mjwhitt Aug 10 '11 at 00:06

1 Answers1

1

Your wrong code is in

// Cursor/Adapter code here to populate list from SQLite DB.

Please post the code

However here are some Hints

  1. Extend ArrayAdapter
  2. Add an ArrayList with all the items inside
  3. Keep in this ArrayList the status of each ViewSwitcher

A simple way would be to make the ArrayList an ArrayList of ViewSwitchers

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • I edited and added some more code. data.myList() returns a Cursor that is pulling data from a sqlite database. Is my problem with using SimpleCursorAdapter? – mjwhitt Aug 10 '11 at 02:08
  • 1
    I extended an adapter and kept the state of the ViewSwitchers, but it was still displaying weird behavior. At that point, I just decided to manually display one of two views rather than trying to get the ViewSwitchers to behave and that worked great. Thanks for pointing me in the right direction. – mjwhitt Aug 18 '11 at 11:40