最初我有一个简单的ListView Activity,它由几个公式组成,顶部还有一个Button。每当我点击那个Button,它就会把我带到另一个Activity,在那里我拖放ListView的内容。 Activity也包含一个Button,每当我点击这个Button时,我在拖放Activity中改变的内容的顺序必须保存在我的初始Activity中。如何保存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 :(得分:1)
以下是您的解决方案:
将新的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
新定位数据。只需访问它,并在您想要使用它时使用。
希望它能清楚。