我在使用拖放编辑列表视图后保存列表时出现问题。Listview Sort with Drag and Drop Android
我'使用sourche代码从这里:Android Drag and Drop List
的代码工作正常,但是当你退出,然后再次打开应用程序的新列表顺序无法保存:
第一个列表视图像这
a
b
c
阻力后拖放
c
b
a
但如果我退出该应用程序,然后再启动它,它仍然是 - > ABC
public class DragNDropListActivity extends ListActivity {
public static String[] mNewPositions;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dragndroplistview);
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);
((DragNDropListView) listView).setPositionListener(mPositionListener);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String selection = (String) getListAdapter().getItem(position);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("selection", selection);
editor.commit();
Intent i = new Intent(this, DkNewsActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case (R.id.Info):
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://search?q=pub:notToSee"));
startActivity(intent);
break;
case (R.id.Rate):
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("rateDone", 1);
editor.commit();
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=notToSee"));
startActivity(intent);
break;
}
return true;
}
@Override
public boolean onPrepareOptionsMenu (Menu menu) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
//set menu rate visible
if (preferences.getInt("rateDone", 0) == 0){
menu.getItem(1).setVisible(true);
}
else {
menu.getItem(1).setVisible(false);
}
return true;
}
private PositionListener mPositionListener=new PositionListener(){
public void tryToScrollInAndroid_1point5(int position) {
ListAdapter adapter = getListAdapter();
if (adapter instanceof DragNDropAdapter) {
getListView().setSelection(position); //android 1.5
}
}
};
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();
//Saving dragNDropList
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();
}
}
}
};
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 = 0xe0103010;
int defaultBackgroundColor;
public void onDrag(int x, int y, ListView listView) {}
public void onStartDrag(View itemView) {
if (itemView != null){itemView.setVisibility(View.INVISIBLE);
defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
itemView.setBackgroundColor(backgroundColor);
ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
if (iv != null) iv.setVisibility(View.INVISIBLE);
}
}
public void onStopDrag(View itemView) {
if (itemView != null){itemView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(defaultBackgroundColor);
ImageView iv = (ImageView)itemView.findViewById(R.id.ImageView01);
if (iv != null) iv.setVisibility(View.VISIBLE);}
}
};
private static String[] mListContent={
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7","Item 8", "Item 9", "Item 10"
,"Item 11", "Item 12", "Item 13", "Item 14", "Item 15", "Item 16", "Item 17","Item 18", "Item 19", "Item 20"};
}
我认为我必须做下的“私人DropListener mDropListener”的东西保存更改和我需要阅读新项目的位置onCreate?
我真的很想这样做,我不知道如何在SharedPreferences中保存数组列表以及如何重新载入数组列表? – Jeff 2012-02-05 18:38:54
我不能使用“putStringSet和getStringSet”,因为我在API级别8 – Jeff 2012-02-05 19:28:05
我找到了解决方案; SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); \t SharedPreferences.Editor editor = preferences.edit(); \t \t StringBuilder sb = new StringBuilder();对于(int i = 0; i
Jeff
2012-02-05 19:59:45