ListView 项目排序/重新排序并保存更改?

发布于 2025-01-07 18:46:06 字数 2377 浏览 1 评论 0原文

我有一个 ListView,我可以对项目重新排序,但每次我退出应用程序并再次启动它时,顺序都会恢复为默认值。如果有人能给我答案,或者提供一些我如何做到这一点的小片段,我将不胜感激。

示例:我有两个项目和两个链接。谷歌和雅虎。我使用拖放和 ArrayList 来实现。一切都很好,只是我不知道如何保存新订单。

我不知道是否还有其他方法可以重新订购这些物品。

我使用 CWAC-TOUCHLIST 来执行此操作。

这是我已经取得的进展:

    private static String[] items={"Google", "Yahoo"};

    private static String[] links={"http://google.com", "http://yahoo.com"};

    private IconicAdapter adapter=null;

    private IconicAdapter1 adapter2=null;

    ArrayList<String> array= new ArrayList<String>(Arrays.asList(items));

    private ArrayList<String> array2=
        new ArrayList<String>(Arrays.asList(links));

   /** Called when the activity is first created. **/
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TouchListView tlv=(TouchListView)getListView();
        adapter=new IconicAdapter();
        adapter2=new IconicAdapter1();
            setListAdapter(adapter);
        setListAdapter(adapter2);

        tlv.setOnItemClickListener(itemclick);
        tlv.setDropListener(onDrop);

   }

    private TouchListView.DropListener onDrop =
        new TouchListView.DropListener() {
            @Override
            public void drop(int from, int to) {
                String item=(String) adapter.getItem(from);

                adapter.remove(item);
                adapter.insert(item, to);

                String link=(String) adapter2.getItem(from);
                adapter2.remove(link);
                adapter2.insert(link, to);
            }
        };

    private TouchListView.OnItemClickListener itemclick =
        new TouchListView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view,
                int from, long id) {

                String item = adapter.getItem(from);

            Toast.makeText(getBaseContext(), "you pick: "+item,
                        Toast.LENGTH_SHORT).show();

                String link = adapter2.getItem(from);
                Intent showContent = new Intent(getApplicationContext(),
                        Web.class);
                showContent.setData(Uri.parse(link));
                startActivity(showContent);
            }
        };

有更聪明的方法吗?

I have a ListView and I can reorder items, but every time I exit from the app and start it again the order goes back to default. I'd appreciate if someone can give me answer and maybe a little snippet of how I can do that.

Example: I have two items and two links. Google and Yahoo. I use drag and drop and ArrayList with that. Everything is fine except I don't know how to save the new order.

I don't know if there is some other way to reorder those items.

I use CWAC-TOUCHLIST to do this.

Here is how far I have got:

    private static String[] items={"Google", "Yahoo"};

    private static String[] links={"http://google.com", "http://yahoo.com"};

    private IconicAdapter adapter=null;

    private IconicAdapter1 adapter2=null;

    ArrayList<String> array= new ArrayList<String>(Arrays.asList(items));

    private ArrayList<String> array2=
        new ArrayList<String>(Arrays.asList(links));

   /** Called when the activity is first created. **/
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TouchListView tlv=(TouchListView)getListView();
        adapter=new IconicAdapter();
        adapter2=new IconicAdapter1();
            setListAdapter(adapter);
        setListAdapter(adapter2);

        tlv.setOnItemClickListener(itemclick);
        tlv.setDropListener(onDrop);

   }

    private TouchListView.DropListener onDrop =
        new TouchListView.DropListener() {
            @Override
            public void drop(int from, int to) {
                String item=(String) adapter.getItem(from);

                adapter.remove(item);
                adapter.insert(item, to);

                String link=(String) adapter2.getItem(from);
                adapter2.remove(link);
                adapter2.insert(link, to);
            }
        };

    private TouchListView.OnItemClickListener itemclick =
        new TouchListView.OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view,
                int from, long id) {

                String item = adapter.getItem(from);

            Toast.makeText(getBaseContext(), "you pick: "+item,
                        Toast.LENGTH_SHORT).show();

                String link = adapter2.getItem(from);
                Intent showContent = new Intent(getApplicationContext(),
                        Web.class);
                showContent.setData(Uri.parse(link));
                startActivity(showContent);
            }
        };

Is there a smarter way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2025-01-14 18:46:06

您必须将列表及其顺序保存到文件或数据库中。
当应用程序启动时,您必须从保存的列表中初始化 ListView。
当您拖动 &删除项目后,您必须保存更改。

您需要更改的方法:
除了已经执行的操作之外,您的 drop 方法还应该将更改存储在文件或数据库中。
您应该传递保存的数组,而不是在 IconicAdapter 和 IconicAdapter2 构造函数 (array & array2) 中传递的数组。

您可以在很多地方阅读如何将数据存储到文件或 SQLite DB。例如,您可以在这里找到一个很好的 SQLite 教程 - http://www. vogella.com/articles/AndroidSQLite/article.html#android_executives(只有前四个部分是相关的,因为您不必使用 ContentProvider)。
如果您选择使用数据库,您可以创建一个包含以下列的表:item、link &命令。在 drop 方法中,您必须更新顺序在 from 和 to 之间的所有行的顺序列(或者如果列表不是太大,您可以简单地更新所有行并将顺序设置为索引)保存列表的数组)。
当您从数据库加载表时,您应该按顺序列对其进行排序。

You have to save the lists with their order either to a file or a DB.
When the application starts you have to initialize the ListViews from the saved lists.
When you drag & drop an item, you have to save the changes.

The method you need to change :
Your drop method should, in addition to what it already does, store the changes in a file or a DB.
Instead of the arrays that you pass in the IconicAdapter and IconicAdapter2 constructors (array & array2), you should pass the saved arrays.

You can read in many places how to store data to a file or a SQLite DB. For example, you can find a good tutorial for SQLite here - http://www.vogella.com/articles/AndroidSQLite/article.html#android_requisites (only the first four sections are relevant, since you don't have to use a ContentProvider).
If you choose to use a DB, you can create a table that contains the following columns : item, link & order. In the drop method you'll have to update the order column of all the rows whose order is between from and to (or if the lists are not too big, you can simply update all the rows and set the order to be the index in the array that holds the list).
When you load the table from the DB you should order it by the order column.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文