У меня проблема с сохранением списка после редактирования просмотра списка с помощью перетаскивания.
Я использую код источника здесь: 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"};
}
Я считаю, что мне нужно что-то сделать в "private DropListener mDropListener", чтобысохранить изменения и мне нужно прочитать новую позицию элемента наСоздать?