我在使用拖放編輯列表視圖後保存列表時出現問題。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