保存 TouchListView 并在下次应用程序打开时打开它

发布于 2025-01-06 07:09:05 字数 3915 浏览 0 评论 0 原文

我需要帮助如何在拖放后保存 ListView 的订单。我的列表中有项目(google 和 yahoo),当用户按下它们时,它会打开 www.google.com 或 www.yahoo.com。

所以现在每次我重新排序列表视图并关闭应用程序时,它都会忘记我之前制作的订单并打开默认订单。

我的代码:

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

        class IconicAdapter extends ArrayAdapter<String> {
            IconicAdapter() {
                super(MainScreen.this, R.layout.row, array);
            }

            public View getView(int position, View convertView,
                                                    ViewGroup parent) {
                View row=convertView;

                    if (row==null) {                                                    
                    LayoutInflater inflater=getLayoutInflater();

                    row=inflater.inflate(R.layout.row, parent, false);
                }

                TextView label=(TextView)row.findViewById(R.id.label);

                label.setText(array.get(position));

                return(row);

            }
        };

            class IconicAdapter1 extends ArrayAdapter<String> {
                IconicAdapter1() {
                    super(MainScreen.this, R.layout.row, array2);
                }

                public View getView(int position, View convertView,
                                                        ViewGroup parent) {
                    View row=convertView;

                    if (row==null) {                                                    
                        LayoutInflater inflater=getLayoutInflater();

                        row=inflater.inflate(R.layout.row, parent, false);
                    }

                    TextView label=(TextView)row.findViewById(R.id.label);

                    label.setText(array.get(position));

                    return(row);

                }
            }

I need help how do I save my ListView's order after drag and drop. My list have items (google and yahoo) and when user press them it opens www.google.com or www.yahoo.com.

So right now every time I reorder my listview and close the app it forgot that order which I made earlier and opens deault order.

My code:

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

        class IconicAdapter extends ArrayAdapter<String> {
            IconicAdapter() {
                super(MainScreen.this, R.layout.row, array);
            }

            public View getView(int position, View convertView,
                                                    ViewGroup parent) {
                View row=convertView;

                    if (row==null) {                                                    
                    LayoutInflater inflater=getLayoutInflater();

                    row=inflater.inflate(R.layout.row, parent, false);
                }

                TextView label=(TextView)row.findViewById(R.id.label);

                label.setText(array.get(position));

                return(row);

            }
        };

            class IconicAdapter1 extends ArrayAdapter<String> {
                IconicAdapter1() {
                    super(MainScreen.this, R.layout.row, array2);
                }

                public View getView(int position, View convertView,
                                                        ViewGroup parent) {
                    View row=convertView;

                    if (row==null) {                                                    
                        LayoutInflater inflater=getLayoutInflater();

                        row=inflater.inflate(R.layout.row, parent, false);
                    }

                    TextView label=(TextView)row.findViewById(R.id.label);

                    label.setText(array.get(position));

                    return(row);

                }
            }

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

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

发布评论

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

评论(1

乙白 2025-01-13 07:09:05

由于您关心的是在列表中存储项目的状态,那么如何将数据存储在 SQLite 数据库 并使用 CursorAdapter?您可以将订单存储为数据库中的一列。

但一般来说,您需要明确保存应用程序状态。请参阅此处了解更多详细信息: http://developer.android.com /guide/topics/data/data-storage.html

Since you're concerned with storing the state of items in a list, how about storing your data in a SQLite database and using a CursorAdapter? you can store your order as a column in the db.

In general though, you'll need to take care of saving your Application state explicitly. Have a look here for more details: http://developer.android.com/guide/topics/data/data-storage.html

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