开发者

ViewSwitcher as a ListView item - how to call .showNext() on correct ViewSwitcher item?

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 开发者_Go百科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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜