使用简单的光标拖动N Drop [英] Drag N Drop utilizing simple cursor

查看:146
本文介绍了使用简单的光标拖动N Drop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CommonsGuy的拖放示例,我基本上是试图将它与Android记事本的例子进行整合。

I'm using CommonsGuy's drag n drop example and I am basically trying to integrate it with the Android notepad example.

拖放N Drop

在2个不同的拖动下拉示例中,我有看到他们都使用了一个静态字符串数组,当我从数据库中获取列表并使用简单的光标适配器。

Out of the 2 different drag n drop examples i've seen they have all used a static string array where as i'm getting a list from a database and using simple cursor adapter.

所以我的问题是如何从简单的光标适配器获取结果到一个字符串数组,但是当列表项被点击时仍然返回行ID,所以我可以将其传递给编辑注释的新活动。

So my question is how to get the results from simple cursor adapter into a string array, but still have it return the row id when the list item is clicked so I can pass it to the new activity that edits the note.

这是我的代码:

Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);

    // Create an array to specify the fields we want to display in the list (only NAME)
    String[] from = new String[]{WeightsDatabase.KEY_NAME};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.weightrows};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = 
            new SimpleCursorAdapter(this, R.layout.weights_row, notesCursor, from, to);
    setListAdapter(notes);

这里是我正在尝试的代码。

And here is the code i'm trying to work that into.

public class TouchListViewDemo extends ListActivity {
private static String[] items={"lorem", "ipsum", "dolor", "sit", "amet",
                                                                "consectetuer", "adipiscing", "elit", "morbi", "vel",
                                                                "ligula", "vitae", "arcu", "aliquet", "mollis",
                                                                "etiam", "vel", "erat", "placerat", "ante",
                                                                "porttitor", "sodales", "pellentesque", "augue", "purus"};
private IconicAdapter adapter=null;
private ArrayList<String> array=new ArrayList<String>(Arrays.asList(items));

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    adapter=new IconicAdapter();
    setListAdapter(adapter);

    TouchListView tlv=(TouchListView)getListView();

    tlv.setDropListener(onDrop);
    tlv.setRemoveListener(onRemove);
}

private TouchListView.DropListener onDrop=new TouchListView.DropListener() {
    @Override
    public void drop(int from, int to) {
            String item=adapter.getItem(from);

            adapter.remove(item);
            adapter.insert(item, to);
    }
};

private TouchListView.RemoveListener onRemove=new TouchListView.RemoveListener() {
    @Override
    public void remove(int which) {
            adapter.remove(adapter.getItem(which));
    }
};

class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
        super(TouchListViewDemo.this, R.layout.row2, array);
    }

    public View getView(int position, View convertView,
                                            ViewGroup parent) {
        View row=convertView;

        if (row==null) {                                                    
            LayoutInflater inflater=getLayoutInflater();

            row=inflater.inflate(R.layout.row2, parent, false);
        }

        TextView label=(TextView)row.findViewById(R.id.label);

        label.setText(array.get(position));

        return(row);
    }
}

}

我知道我要求很多,但正确方向的一点将会有所帮助。
谢谢

I know i'm asking for a lot, but a point in the right direction would help quite a bit! Thanks

推荐答案

您不能将 SimpleCursorAdapter code> TouchListView ,原因很简单,就是无法修改 SimpleCursorAdapter 适配器必须更改其数据以反映和拖放操作。

You cannot use a SimpleCursorAdapter with TouchListView, for the simple reason that a SimpleCursorAdapter cannot be modified. The Adapter has to change its data to reflect and drag and drop operations.

简单但有些笨重的解决方案要遍历 Cursor 并创建一个 ArrayList 的数据,然后使用 ArrayList ArrayAdapter TouchListView

The simple but somewhat clunky solution would be to iterate over the Cursor and create an ArrayList of something out of that data, then use that ArrayList with an ArrayAdapter and TouchListView.

光滑但复杂的解决方案将是创建一个装饰 ListAdapter ,它位于 TouchListView 和您的 SimpleCursorAdapter ,并了解您的拖放数据更改并即时应用。例如,如果用户交换位置5和6,当 TouchListView 调用 getView()获取位置5时,装饰适配器将知道从 SimpleCursorAdapter 中获取位置6的行。

The slick but complex solution would be to create a decorating ListAdapter that sits between TouchListView and your SimpleCursorAdapter and knows about your drag and drop data changes and applies them on the fly. For example, if the user swaps positions 5 and 6, when TouchListView calls getView() to get position 5, the decorating adapter would know to get position 6's row from the SimpleCursorAdapter.

这篇关于使用简单的光标拖动N Drop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆