Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8218595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:51:09+00:00 2026-06-07T12:51:09+00:00

I have a GridView with multiple items, but the items must be kept selected

  • 0

I have a GridView with multiple items, but the items must be kept selected once the the onClickListener is called.How can i achive this?

I’v already tried v.setSelected(true) but it doesnt seem to work.

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            // Toast.makeText(Project.this, "Red" + position,
            // Toast.LENGTH_SHORT).show(); //position = al catelea element
            v.setPressed(true);
            if (bp == 2) {
                if (position == 0) {
                Square.setSex(R.drawable.girl_body2v);
                Square2.setHair(R.drawable.girl_hair_01v);
                SquareAccesories.setAcc(R.drawable.girl_accessories_01v);
                SquareEyes.setEyes(R.drawable.eyes_1v);
                SquareLips.setLips(R.drawable.lip_1v);
                Square3.setDress(R.drawable.girl_tops_01v);
                SquareShoes.setShoes(R.drawable.girl_shoes_01v);
                SquarePants.setPants(R.drawable.girl_bottom_01v);
                setS(2);

This is a small part of the code for the onClickListener because i have lots of cases.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:51:11+00:00Added an answer on June 7, 2026 at 12:51 pm

    The concept that you want to achieve is possible, but not like the way you are working now.

    The best and easiest solution would be to keep track of the states of the clicked items and give them the correct layout inside the adapter. I have set up a little example:

    Activity

    public class StackOverFlowActivity extends Activity {
        GridView gridView;
        MyCustomAdapter myAdapter;
        ArrayList<GridObject> myObjects;
    
        static final String[] numbers = new String[] { "A", "B", "C", "D", "E",
                "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                "S", "T", "U", "V", "W", "X", "Y", "Z" };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            myObjects = new ArrayList<GridObject>();
            for (String s : numbers) {
                myObjects.add(new GridObject(s, 0));
            }
    
            gridView = (GridView) findViewById(R.id.gridView1);
    
            myAdapter = new MyCustomAdapter(this);
    
            gridView.setAdapter(myAdapter);
            gridView.setOnItemClickListener(new OnItemClickListener() {
    
                @Override
                public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                    myObjects.get(position).setState(1);
                    myAdapter.notifyDataSetChanged();
                }
            });
        }
    
        static class ViewHolder {
            TextView text;
        }
    
        private class MyCustomAdapter extends BaseAdapter  {
    
            private LayoutInflater mInflater;
    
            public MyCustomAdapter(Context context) {
                mInflater = LayoutInflater.from(context);
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                GridObject object = myObjects.get(position);
                ViewHolder holder;
    
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
                    holder = new ViewHolder();
                    holder.text = (TextView) convertView.findViewById(R.id.text);
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
    
                holder.text.setText(object.getName());
    
                if (object.getState() == 1) {
                    holder.text.setBackgroundColor(Color.GREEN);
                } else {
                    holder.text.setBackgroundColor(Color.BLUE);
                }
                return convertView;
            }
    
            @Override
            public int getCount() {
                return myObjects.size();
            }
    
            @Override
            public Object getItem(int position) {
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
        }
    }
    

    GridObject

    public class GridObject {
    
        private String name;
        private int state;
    
        public GridObject(String name, int state) {
            super();
            this.name = name;
            this.state = state;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getState() {
            return state;
        }
    
        public void setState(int state) {
            this.state = state;
        }   
    }
    

    Main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <GridView
            android:id="@+id/gridView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:columnWidth="50dp"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" >
        </GridView>
    
    </LinearLayout>
    

    list_item_icon_text

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview control that can get loaded with multiple pages worth of
I have a ListView (GridView) with multiple columns and so far I can sort
I have multiple buttons in gridview. How can i determine which particular button on
I have a gridview where I can select multiple rows by pressing a control
I have a gridview with multiple rows, each has a Update button and I
I have a GridView what I fill through a LINQ expression. Something like this:
I have a gridview that displays items details, I added two template fields one
I have a gridview ...with multiple columns. If I click on one column, it
I have a master-detail page, in which I use GridView to display multiple rows
I have multiple radio buttons columns in my gridview, I want to select one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.