2011-08-25 68 views
2

最初我有一個簡單的ListView活動,它由幾個公式組成,並且頂部有一個Button。每當我點擊該按鈕時,它就會將我帶到另一個活動,我在哪裏拖放ListView的內容。這個Activity還包含一個Button,每當我點擊這個Button時,我在拖放中所做的更改的內容順序必須保存在我的初始Activity中。如何保存ListView的內容我已經在我的拖放到我的初始活動?在拖放後保存ListView的內容

代碼:我的初始活動和我的下一個活動是拖放。

public class FormulaActivity extends Activity implements OnClickListener { 

private ListView listview; 
private ArrayList<ListContents> mListItem; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main1); 
    ImageButton btn=(ImageButton)findViewById(R.id.imageButton00); 
    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i=new Intent(FormulaActivity.this,DragNDropListActivity.class); 
      startActivity(i); 


     } 
    }); 
    listview = (ListView) findViewById(R.id.list_view); 
    mListItem = ListContents.getItems(); 
    listview.setAdapter(new ListAdapter(FormulaActivity.this, R.id.list_view, 
      mListItem)); 

} 

@Override 
public void onClick(View v) { 

} 

// ***ListAdapter*** 
private class ListAdapter extends ArrayAdapter<ListContents> { 
    private ArrayList<ListContents> mList; 
    public ListAdapter(Context context, int textViewResourceId, 
      ArrayList<ListContents> list) { 
     super(context, textViewResourceId, list); 
     this.mList = list; 

    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     try { 
      if (view == null) { 
       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = vi.inflate(R.layout.list, null); 
      } 
      final ListContents listItem = mList.get(position); 
      if (listItem != null) { 
       // setting list_item views 
       ((TextView) view.findViewById(R.id.tv_name)) 
         .setText(listItem.getName()); 
       view.setOnClickListener(new OnClickListener() { 


     public void onClick(View arg0) { // --clickOnListItem 
         Intent myIntent = new Intent(FormulaActivity.this, 
           Activity2.class); 
         myIntent.putExtra("NAME", listItem.getName()); 
         startActivity(myIntent); 
         //finish(); 
        } 
       }); 
      } 
     } catch (Exception e) { 
      Log.i(FormulaActivity.ListAdapter.class.toString(), e.getMessage()); 
     } 
     return view; 
    } 
} 

}


public class DragNDropListActivity extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.dragndroplistview); 
    Button mbtn=(Button)findViewById(R.id.button12); 
    mbtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
     Intent i=new Intent(DragNDropListActivity.this,FormulaActivity.class); 
     startActivity(i); 
     finish(); 
     } 
    }); 
    ArrayList<String> content = new ArrayList<String>(mListContent.length); 
    for (int i=0; i < mListContent.length; i++) { 
     content.add(mListContent[i]); 
    } 

    setListAdapter(new DragNDropAdapter(this, new int[]{R.layout.dragitem}, new int[]{R.id.TextView01}, content));//new DragNDropAdapter(this,content) 
    ListView listView = getListView(); 

    if (listView instanceof DragNDropListView) { 
     ((DragNDropListView) listView).setDropListener(mDropListener); 
     ((DragNDropListView) listView).setRemoveListener(mRemoveListener); 
     ((DragNDropListView) listView).setDragListener(mDragListener); 
    } 
} 

private DropListener mDropListener = 
    new DropListener() { 
    public void onDrop(int from, int to) { 
     ListAdapter adapter = getListAdapter(); 
     if (adapter instanceof DragNDropAdapter) { 
      ((DragNDropAdapter)adapter).onDrop(from, to); 
      getListView().invalidateViews(); 
     } 
    } 
}; 

private RemoveListener mRemoveListener = 
    new RemoveListener() { 
    public void onRemove(int which) { 
     ListAdapter adapter = getListAdapter(); 
     if (adapter instanceof DragNDropAdapter) { 
      ((DragNDropAdapter)adapter).onRemove(which); 
      getListView().invalidateViews(); 
     } 
    } 
}; 

private DragListener mDragListener = 
    new DragListener() { 

    int backgroundColor = 0xe0103000; 

     public void onDrag(int x, int y, ListView listView) { 
      // TODO Auto-generated method stub 
     } 

     public void onStartDrag(View itemView) { 
      itemView.setVisibility(View.INVISIBLE); 
      itemView.setBackgroundColor(backgroundColor); 
      ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01); 
      if (iv != null) iv.setVisibility(View.VISIBLE); 
     } 

     public void onStopDrag(View itemView) { 
      itemView.setVisibility(View.VISIBLE); 
      itemView.setBackgroundColor(backgroundColor); 
      ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01); 
      if (iv != null) iv.setVisibility(View.VISIBLE); 
     } 

}; 

private static String[] mListContent={"BMI", "Infuusberekening: druppels per minuut", "Infuusberekening: resterende tijd","Injecteren: IE-aanduiding", "Injecteren: mg/ml-aanduiding", "Injecteren: %-aanduiding", 
            "Lichaamsoppervlakte", "Medicatieberekening voor gewicht", 
            "Oplossen: Hoeveelheid percentage", 
            "Oplossen: Hoeveelheid promillage", 
            "Oplossen: Percentage in oplossing", 
            "Oplossen: Promillage in oplossing", 
            "PCA-pomp", 
            "Procenten: delen", 
            "Procenten: percentage", 
            "Promillage: delen", 
            "Promillage: percentage", 
            "Spuitenpomp", 
            "Verdunnen", 
            "Voedingspomp: ml per uur", 
            "Voedingspomp: resterende tijd", 
            "Zuurstofberekening"}; 

}

+0

你能分享更多的信息嗎?丟棄事件調用時,您可以重新排序數據。更多信息?把代碼或更多的細節... –

+0

此鏈接將有助於你http://stackoverflow.com/questions/7136386/adding-items-to-expandable-list-view-by-drag-and-drop-android – Randroid

+0

@pankaj:我認爲上面提到的代碼可能會幫助你解決我的問題。 –

回答

2

這裏是您的解決方案:

聲明新的String數組作爲

public static String[] mNewPositions; 

添加一些代碼在你mDropListener

private DropListener mDropListener = 
     new DropListener() { 
    public void onDrop(int from, int to) { 
     ListAdapter adapter = getListAdapter(); 
     if (adapter instanceof DragNDropAdapter) { 
      ((DragNDropAdapter)adapter).onDrop(from, to); 
      getListView().invalidateViews(); 

      mNewPositions = new String[adapter.getCount()]; //Initialize your new items storage 

       for(int i=0; i < adapter.getCount(); i++) { 
        //Implement here your logic for save positions 
        mNewPositions[i] = adapter.getItem(i).toString(); 
       }    
     } 
    } 
}; 

現在你有mNewPositions新的定位數據。只需訪問它並在您想使用它時使用。

希望它會明確。

+0

我發現你的解決方案絕對正確。謝謝:-) –

+0

@Vivek歡迎!並嘗試upvote也答案:) –