【问题标题】:how to get index or position of listview control如何获取列表视图控件的索引或位置
【发布时间】:2014-12-05 19:15:58
【问题描述】:

大家好,我是 android 编程新手。我想创建一个益智游戏,用户将通过在屏幕上拖动它们来对 html 未排序的标签进行排序。我正在使用 listview 控件来存储未排序的 html 标签。我已经实现了以下库: https://github.com/terlici/DragNDropList

现在我试图在用户删除列表项后获取列表项的位置或索引,以便我可以将用户排序的列表项与实际排序的项目进行比较。

这是我的代码 Main.java

PuzzleManager pm = new PuzzleManager(this);
pm.insertPuzzle("<html>,</h1>,<h1>,</html>,<body>,</body>", "<html>,<body>,<h1>,</h1>,</body>,</html>");
Cursor cursor = pm.getPuzzle(1);
pm.close();
String s = cursor.getString(1); 
List<String> puzzleList = Arrays.asList(s.split(","));
ArrayList<Map<String, Object>> items = new ArrayList<Map<String, Object>>();

for(int i = 0; i < puzzleList.size(); ++i) {
    HashMap<String, Object> item = new HashMap<String, Object>();
    item.put("puzzle", puzzleList.get(i));
    items.add(item);
}

DragNDropListView list = (DragNDropListView)findViewById(android.R.id.list);
DragNDropSimpleAdapter adaptor = new DragNDropSimpleAdapter(this,
        items,
        R.layout.row,
        new String[]{"puzzle"},
        new int[]{R.id.text},
        R.id.handler);
list.setDragNDropAdapter(adaptor);

Main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<com.terlici.dragndroplist.DragNDropListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

</LinearLayout>

行.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80px"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    >

    <ImageView
        android:id="@+id/handler"
        android:layout_width="60px"
        android:layout_height="60px"
        android:src="@android:drawable/btn_star_big_on"
        android:layout_marginLeft="8px"
        />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

提前致谢

【问题讨论】:

  • “DragNDropListView 提供拖放事件监听器”:list.setOnItemDragNDropListener(...)。使用它来获取丢弃项目的索引(id

标签: java android listview drag-and-drop draggable


【解决方案1】:
list.setOnItemDragNDropListener(new DragNDropListener(){
                public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id)
                {
                    super.onItemDrop(parent, view, startPosition, endPosition, id);
                    Log.i("long clicked","pos"+" "+endPosition+" id "+id);
                }

        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2017-03-19
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多