【问题标题】:ListView Reorder ItemsListView 重新排序项目
【发布时间】:2012-02-07 23:43:07
【问题描述】:

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

所以我想在应用程序中更改我的 ListView 项目顺序,但我不知道该怎么做。我猜拖放太难构建,但是一些箭头按钮怎么样?你可以向上按下并且项目向上移动一行?以及如何让应用记住下次打开时的顺序,每件商品都按照我移动它们的顺序排列?

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

        });

    }
}

有人知道如何让它工作吗? :)

【问题讨论】:

  • 未排序更像用户必须能够更改顺序,例如 user1 想要列表的第 3 项但 user2 想要第 2 项。就像他们必须能够更改顺序,哪个项目在顶部,哪个项目在其下方,等等。:)

标签: android listview android-listview listviewitem


【解决方案1】:

在 Android 3.0+ 上实现对列表的拖放功能很容易。检查文档:

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

如果您想要预蜂窝解决方案,试试这个:

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

【讨论】:

  • 我的应用使用 android 2.2 及更高版本。我尝试了那个 cwac-touchlist 但我没有得到它。 :) 我无法将该库导入我的应用程序。
  • 页面上有详细说明。
  • 是的,但我不明白那些 :) 如果我得到了那份工作,我想我仍然需要做那个 sharedpreferences 的事情吗?
  • 不,这不会为您提供拖放功能。您需要一个自定义视图,就像我链接的那样。 ;)
  • 您对如何让应用记住新订单有什么想法吗?就像我重新排序项目并关闭应用程序一样。然后我再次打开应用程序,项目应该与我关闭应用程序时的顺序相同。
【解决方案2】:

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

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

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

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

编辑

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

【讨论】:

  • 如果我像使用 SqlLite 创建数据库那样进行更改,然后当我启动应用程序时,它会通过 Internet 从我的服务器下载该数据库一次,这是否有效?因为这样我可以让应用程序变小,对吧? :)
  • @Eljas 和SQLLite 你在设备上创建数据库,而不是在服务器或任何东西上。它可以让你用你的Link 列表做任何你想做的事情。但是,如果您不打算在列表中添加add/remove 链接,我仍然建议您至少在短期内查看SharedPreferences。
  • 你知道关于 SharedPreferences 的任何好的教程吗,因为我不明白。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 2010-11-15
  • 1970-01-01
  • 2015-08-06
相关资源
最近更新 更多