2

I have made grid view in which I have to do multiple selection of items.But I dont want long tap functionality.I simply want that on single tap the multiple items can be selected.Grid view is under fragment.

This is my fragment class:

public class FragmentOrder extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //View view = inflater.inflate(R.layout.g, null); View view = inflater.inflate(R.layout.gridview,null); final GridView listView = (GridView) view.findViewById(R.id.mainGrid); listView.setAdapter(new OrderGridViewAdapter(MainActivity.this)); //int setSelected = 0; listView.setSelected(true); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } }); return view; } } 

This is my Adapter class:

public class OrderGridViewAdapter extends BaseAdapter{ private Context MContext; public OrderGridViewAdapter(Context C){ MContext = C; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View myView; LayoutInflater inflater = (LayoutInflater)MContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); myView = inflater.inflate(R.layout.grid_items_ontap, null); // Add The Image!!! ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap); iv.setImageResource(mThumbIds[position]); // Add The Text!!! TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap); tv.setText(names[position] ); return myView; } private Integer[] mThumbIds = { R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car,R.drawable.car,R.drawable.car }; private String[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""}; } 

Any suggestion will be appreciated. Thanks.

1 Answer 1

5

This is a rough solution of what you need to do:

1) Maintain a list which will contain the positions of currently selected items.

private ArrayList<Integer> mSelected = new ArrayList<Integer>(); 

When you click on item (select item), add it to the list. When you click on item again (deselect item), remove from the list.

 public void onItemSelect(AdapterView<?> parent, View v, int pos, long id) { Integer position = new Integer(pos); if(mSelected.contains(position)) { mSelected.remove(position); // remove item from list // update view (v) state here // eg: remove highlight } else { mSelected.add(position); // add item to list // update view (v) state here // eg: add highlight } } 

2) You have to update the view, to show if item is selected or not, I will add code (+comments) on where to do that.

3) In the end, the list will contain all items which have been selected.

Here is the code which shows you where to place the above code.

Code:

public class FragmentOrder extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //View view = inflater.inflate(R.layout.g, null); View view = inflater.inflate(R.layout.gridview,null); final GridView listView = (GridView) view.findViewById(R.id.mainGrid); final OrderGridViewAdapter adapter = new OrderGridViewAdapter(MainActivity.this) listView.setAdapter(adapter); //int setSelected = 0; listView.setSelected(true); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { adapter.onItemSelect(arg0, arg1, arg2, arg3); } }); return view; } } 

Adapter:

public class OrderGridViewAdapter extends BaseAdapter{ private Context MContext; private ArrayList<Integer> mSelected = new ArrayList<Integer>(); public OrderGridViewAdapter(Context C){ MContext = C; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public void onItemSelect(AdapterView<?> parent, View v, int pos, long id) { Integer position = new Integer(pos); if(mSelected.contains(position)) { mSelected.remove(position); // update view (v) state here // eg: remove highlight } else { mSelected.add(position); // update view (v) state here // eg: add highlight } } @Override public View getView(int position, View convertView, ViewGroup parent) { View myView; LayoutInflater inflater = (LayoutInflater)MContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); myView = inflater.inflate(R.layout.grid_items_ontap, null); // Add The Image!!! ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap); iv.setImageResource(mThumbIds[position]); // Add The Text!!! TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap); tv.setText(names[position] ); // Set view highlight here, based on if it is selected or not.. if(mSelected.contains(position)) { // update view (v) state here // eg: add highlight } else { // update view (v) state here // eg: remove highlight } return myView; } private Integer[] mThumbIds = { R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car, R.drawable.car,R.drawable.car,R.drawable.car }; private String[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""}; } 

Update: To update the view, you should read about how to change properties of the view programatically. For example, if you want to change the background color:

v.setBackgroundColor(Color.parseColor("#000000")); // change to black 
Sign up to request clarification or add additional context in comments.

4 Comments

How to maintain a list?
Yes,I have seen but how I know to update the list which will contain the positions o f selected items.As u said..?
One thing how i will update the view as whatever I am doing for changing the background of selected item is from xml file..?
hey, I am sorry I am having trouble understanding what you mean. I have updated my answer to be more clear. Also, answers on stackoverflow are generally rough guidelines on how to solve your problem. No one will write the entire code for you..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.