Vamshi
Vamshi

Reputation: 1495

How to get the selected items from listview when clicking on button

I have a custom listview and i am selecting multiple items in the list, now i need to get those item names when clicking on a button the button will calls listfunction() method, you can see this in the below code.

public class Places extends Activity {

    private ListView listView;

    private static int selectedListItem = -1;
    private Handler mHandler = new Handler();
    private static Vector<String> data; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_custom_list_view);
        data = new Vector<String>();

        // Add data as per your requirement   
        data.add("one");
        data.add("two");
        data.add("three");
        data.add("four");
        data.add("five");
        data.add("six");

        listView = (ListView)findViewById(R.id.ListView01);
        listView.setDivider(null);

        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //System.out.println(EfficientAdapter.saveState.get(position));
                selectedListItem = position;
                ((EfficientAdapter)listView.getAdapter()).notifyDataSetChanged();
                if(EfficientAdapter.saveState.get(position)=="selected"){
                    EfficientAdapter.saveState.put(position,"unselected");
                }else
                EfficientAdapter.saveState.put(position,"selected");  
                mHandler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        // call any new activity here or do any thing you want here         

                    }
               }, 200L);
            }
        });

        listView.setAdapter(new EfficientAdapter(getApplicationContext()));   
    }

    private static class EfficientAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        static HashMap<Integer,String> saveState=new HashMap<Integer,String>();

        public EfficientAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
            for(int i=0;i<data.size();i++)
            {
               saveState.put(i,"unselected");
            }

        }

        public int getCount() {
            return data.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

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

            ViewHolder holder;

            if (convertView == null || convertView.getTag() == null) {
                convertView = mInflater.inflate(R.layout.places_custom_row_view, null);
                holder = new ViewHolder();

                holder.txtName = (TextView) convertView
                    .findViewById(R.id.name);

                convertView.setTag(holder);
            } else {
                 holder = (ViewHolder) convertView.getTag();
            }


            if(saveState.get(position).equals("selected"))
                holder.txtName.setBackgroundResource(R.drawable.cellbghover);  

             else
                holder.txtName.setBackgroundResource(R.drawable.cellbgnew);

            holder.txtName.setText(data.get(position)); 

            return convertView;
        }
    }

    static class ViewHolder {
        TextView txtName;
    }

    public void listfunction(View button) {
        System.out.println("listfunction items are::::"+selectedListItem);

    }
    }

Upvotes: 1

Views: 2481

Answers (2)

AbhishekB
AbhishekB

Reputation: 2075

Use a global array to store the index of all the items clicked or touched in the list view by using the following:

listview.setOnItemClickListener(
  new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> p, View view, int position,long id) {}
  }
);

where position will be the index of the item starting from 0.then after clicking on button use the array to retrieve the positions of the items clicked

Upvotes: 1

nisha.113a5
nisha.113a5

Reputation: 2024

Custom listview should have custom adapter. In custom adapter you can find method named getView() along with view, position and parentgroup. When you are attempting click event, position will represent the index of the currently selected item.

public class MyArrayAdapter extends ArrayAdapter<String> {
    @Override
    public View getView(final int position, View convertView,ViewGroup parent) {
            }
}

Upvotes: 1

Related Questions