1

我正在使用 CommonsGuy 的拖放示例,我基本上是在尝试将它与 Android 记事本示例集成。

拖放

在我见过的 2 个不同的拖放示例中,它们都使用了静态字符串数组,因为我从数据库中获取列表并使用简单的光标适配器。

所以我的问题是如何将简单光标适配器的结果转换为字符串数组,但在单击列表项时仍然返回行 ID,以便我可以将其传递给编辑注释的新活动。

这是我的代码:

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);

这是我正在尝试处理的代码。

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);
    }
}

}

我知道我要求很多,但是朝着正确的方向前进会很有帮助!谢谢

4

1 回答 1

3

您不能使用 a SimpleCursorAdapterwith TouchListView,原因很简单, aSimpleCursorAdapter不能被修改。必须更改其Adapter数据以反映和拖放操作。

简单但有点笨拙的解决方案是迭代Cursor并从该数据中创建一个ArrayListof ,然后将其ArrayList与 an 一起ArrayAdapter使用TouchListView

巧妙但复杂的解决方案是创建一个ListAdapter位于您和TouchListView您之间的装饰,SimpleCursorAdapter并且知道您的拖放数据更改并动态应用它们。例如,如果用户交换位置 5 和 6,当TouchListView调用getView()获取位置 5 时,装饰适配器会知道从SimpleCursorAdapter.

于 2011-02-18T23:23:41.180 回答