问题
I tried to look answers but I couldn't find any.
So I want to change my ListView items order in app and I don't have any idea how to do that. I guess Drag and Drop is too hard to build but how about some arrow buttons where you can press up and item move one row up? And how to make app to remember order that next time I open it every item are in order where I moved them?
I have my ListView Items in their own file (values->arrays.xml). And here is my ListView activity:
public class MainScreen extends ListActivity implements OnClickListener{
/** Called when the activity is first created. **/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] links = getResources().getStringArray(R.array.links);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(),
R.array.items, R.layout.rowlayout));
getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String content = links[position];
Intent showContent = new Intent(getApplicationContext(),
Web.class);
showContent.setData(Uri.parse(content));
startActivity(showContent);
}
});
}
}
Anyone have idea how to make it work? :)
回答1:
As far as I know, String resources are static so you cannot manipulate the xml in the code (to change the order, add / remove items ..etc).
But I can think of two solutions for your problem.
Save the order you want in the SharedPreferences and apply it to your
String[]in theonCreate()(e.g. save the sequence of your string array index-- creepy but would work, I think)Learn and start using a SqlLite database if that s what you are trying to avoid :)
Edit
To see how to use SharedPreferences : check the code snippet here http://developer.android.com/guide/topics/data/data-storage.html
回答2:
It's easy to implement drag/drop functionality to a list on Android 3.0+. Check the docs:
http://developer.android.com/guide/topics/ui/drag-drop.html
If you want a pre-honeycomb solution, try this:
https://github.com/commonsguy/cwac-touchlist
来源:https://stackoverflow.com/questions/9185802/listview-reorder-items