我使用的是CommonsGuy的拖放n放置示例,我基本上正在嘗試將它與Android記事本示例集成。使用簡單光標拖動N滴
出了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);
}
}
}
我知道我要求很多,但在正確的方向的一個點,將有助於頗有幾分! 謝謝
更新:到目前爲止,我做的是創建一個包含原始名稱的字符串數組,然後拖拽drop數組以找出哪些位置已更改,然後使用這些更改更新rowId數組。醜,但它的作品。現在我將嘗試改進它以更高效地運行。 – Cameron 2011-02-21 00:09:15