ListView重新排序项目

时间:2012-02-07 23:43:07

标签: android listview android-listview listviewitem

我试着寻找答案,但我找不到任何答案。

所以我想在app中更改我的ListView项目顺序,我不知道该怎么做。我想Drag and Drop太难建了,但是有些箭头按钮怎么样可以按下并且项目向上移动一行?以及如何使应用程序记住下次我打开它的每个项目的顺序是按照我移动它们的顺序?

我将ListView项目放在他们自己的文件中(values-> arrays.xml)。这是我的ListView活动:

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);
            }

        });

    }
}

任何人都知道如何让它发挥作用? :)

2 个答案:

答案 0 :(得分:3)

在Android 3.0+上实现拖放功能很容易。查看文档:

http://developer.android.com/guide/topics/ui/drag-drop.html

如果您需要预蜂窝解决方案,请尝试以下方法:

https://github.com/commonsguy/cwac-touchlist

答案 1 :(得分:1)

据我所知,代码中String resources are static so you cannot manipulate the xml(更改顺序,添加/删除项目..等)。

但我可以为您的问题考虑两种解决方案。

  1. SharedPreferences中保存您想要的订单并将其应用到String[]中的onCreate()(例如,保存字符串数组索引的序列 - 令人毛骨悚然但是会工作,我认为)

  2. 了解并开始使用SqlLite数据库,如果这是您要避免的:)

  3. 修改

    要了解如何使用SharedPreferences:请查看此处的代码段http://developer.android.com/guide/topics/data/data-storage.html

相关问题