Nitin
Nitin

Reputation: 1986

How can I change the position of Item selected in list View in android

I am making an application in which I have to change the position of the selected item in list view to first position and set other items one position less than current How can i do this. Can any body suggest me some tutorial or any suggestion.

Upvotes: 2

Views: 3549

Answers (1)

user831722
user831722

Reputation:

Use Adapter, and arraylist on item click remove the item from that position. out it at the 0th position and use notifyDataSetChanged to reaarange the listview

Example

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Main_Screen extends ListActivity implements OnItemClickListener{
    ArrayAdapter arrayAdapter = null;
    /** Called when the activity is first created. */
    Context context = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        penList.add("MONT Blanc");
        penList.add("Gucci");
        System.out.println("...1...");
        arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, penList);
        arrayAdapter.add("last by adapter");


        setListAdapter(arrayAdapter);
        penList.add("Last By list");
        arrayAdapter.add("last by adapter2");

        getListView().setTextFilterEnabled(true);
        ListView lv = getListView();
        this.registerForContextMenu(lv);

        lv.setOnItemClickListener(this);


    }



    static ArrayList<String> penList   = new ArrayList<String>();
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        String str = penList.get(arg2);
        penList.remove(arg2);
        penList.add(0, str);
        arrayAdapter.notifyDataSetChanged();
    }




}

Upvotes: 1

Related Questions