ListView Reorder Items

别说谁变了你拦得住时间么 提交于 2019-12-10 10:33:33

问题


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.

  1. Save the order you want in the SharedPreferences and apply it to your String[] in the onCreate() (e.g. save the sequence of your string array index-- creepy but would work, I think)

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!