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

を変更できないという単純な理由から、SimpleCursorAdapterwithを使用することはできません。は、操作を反映およびドラッグアンドドロップするためにデータを変更する必要があります。TouchListViewSimpleCursorAdapterAdapter

単純ですがやや不格好な解決策は、を繰り返してそのデータから何かCursorを作成し、それをとで使用することです。ArrayListArrayListArrayAdapterTouchListView

洗練されているが複雑な解決策は、とListAdapterの間に位置TouchListViewSimpleCursorAdapter、ドラッグアンドドロップデータの変更を認識してその場で適用する装飾を作成することです。たとえば、ユーザーが位置5と6を入れ替えた場合、位置5を取得するためにTouchListView呼び出したときgetView()に、装飾アダプターは位置6の行をから取得することを認識しますSimpleCursorAdapter

于 2011-02-18T23:23:41.180 に答える