2011-02-18 32 views
1

我使用的是CommonsGuy的拖放n放置示例,我基本上正在嘗試將它與Android記事本示例集成。使用簡單光標拖動N滴

Drag N Drop

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

}

我知道我要求很多,但在正確的方向的一個點,將有助於頗有幾分! 謝謝

+0

更新:到目前爲止,我做的是創建一個包含原始名稱的字符串數組,然後拖拽drop數組以找出哪些位置已更改,然後使用這些更改更新rowId數組。醜,但它的作品。現在我將嘗試改進它以更高效地運行。 – Cameron 2011-02-21 00:09:15

回答

3

您不能使用SimpleCursorAdapterTouchListView,原因很簡單,因爲無法修改SimpleCursorAdapterAdapter必須更改其數據以反映和拖放操作。

簡單,但有些笨拙的解決辦法是遍歷Cursor和創造的東西的ArrayList指出的數據,然後使用該ArrayListArrayAdapterTouchListView

的光滑,但複雜的解決方案是創建一個裝飾ListAdapterTouchListView和你SimpleCursorAdapter之間坐着知道你的拖放數據的變化並應用它們的飛行。例如,如果用戶交換位置5和6,當TouchListView調用getView()以獲取位置5時,裝飾適配器將知道從SimpleCursorAdapter獲得位置6的行。

+0

謝謝!只是我想回應的那個人) 是的,我擔心這不會是一個太簡單的解決方案。看着它後,它似乎需要一個自定義適配器。感謝您指點我正確的方向。我想我現在有一個關於如何去做的想法。我會更新這個線程,如果我得到一個工作解決方案。 – Cameron 2011-02-18 23:42:46