2

I tried to look answers but I couldn't find any.

So I want to change my ListView items order in app and I don't have any idea how to do that. I guess Drag and Drop is too hard to build but how about some arrow buttons where you can press up and item move one row up? And how to make app to remember order that next time I open it every item are in order where I moved them?

I have my ListView Items in their own file (values->arrays.xml). And here is my ListView activity:

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

        });

    }
}

Anyone have idea how to make it work? :)

Eljas
  • 1,187
  • 4
  • 17
  • 37
  • Not sorted more like user have to be able to change order like if user1 want 3rd item top of the list but user2 want 2nd item to top. Like they have to be able to change order which item is top and which one below that etc. :) – Eljas Feb 08 '12 at 01:46

2 Answers2

3

It's easy to implement drag/drop functionality to a list on Android 3.0+. Check the docs:

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

If you want a pre-honeycomb solution, try this:

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

Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
  • My app use android 2.2 and above. And I tried that cwac-touchlist but I didn't get it. :) I couldn't import that library to my app. – Eljas Feb 08 '12 at 01:49
  • There are detailed instructions on the page. – Christopher Perry Feb 08 '12 at 01:54
  • Yeah, but I didn't understand those :) If I get that work I guess I still have to do that sharedpreferences thing? – Eljas Feb 08 '12 at 02:48
  • No, that won't give you drag/drop functionality. You need a custom View for that, like the one I linked. ;) – Christopher Perry Feb 08 '12 at 23:23
  • Do you have any ideas how to make app remember new order? Like I reorder items and close app. Then I open app again and items should be same order how they was when I close app. – Eljas Feb 09 '12 at 01:03
  • It depends on how your data is stored. In my app mooLa, the user's data is stored in a sqlite database file. I'm keeping track of Account order in the Accounts table, and retrieving my data ordered by this. It really depends on how you are saving your data. – Christopher Perry Feb 09 '12 at 02:21
  • I make static strings in activity right now. If that makes any sense? :) See my new question: http://stackoverflow.com/questions/9204485/drag-and-drop-with-cwac-touchlist – Eljas Feb 09 '12 at 02:25
1

As far as I know, String resources are static so you cannot manipulate the xml in the code (to change the order, add / remove items ..etc).

But I can think of two solutions for your problem.

  1. Save the order you want in the SharedPreferences and apply it to your String[] in the onCreate() (e.g. save the sequence of your string array index-- creepy but would work, I think)

  2. Learn and start using a SqlLite database if that s what you are trying to avoid :)

Edit

To see how to use SharedPreferences : check the code snippet here http://developer.android.com/guide/topics/data/data-storage.html

Community
  • 1
  • 1
Orkun
  • 6,998
  • 8
  • 56
  • 103
  • Is this working if I change like I make database with SqlLite and then when I start app it download that database once over internet from my server? Because this way I can get app size smaller, right? :) – Eljas Feb 08 '12 at 01:43
  • @Eljas with `SQLLite` u create a DB on the device, not on the server or anything. It would allow you to do anything u want with your `Link` list. BUT, I still suggest you have a look at the `SharedPreferences` if you are not planning to `add/remove` links from your list, in the short run at least. – Orkun Feb 08 '12 at 10:41
  • Do you know any good tutorials about that SharedPreferences because I don't get it. :) – Eljas Feb 14 '12 at 00:06