2

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?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Eljas
  • 1,187
  • 4
  • 17
  • 37

1 Answers1

2

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.

Eran
  • 387,369
  • 54
  • 702
  • 768