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

  • SEARCH
  • Home
  • 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 5984117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:19:43+00:00 2026-05-22T22:19:43+00:00

I have one GridView with 3 Column and 3 Rows I want to change

  • 0

I have one GridView with 3 Column and 3 Rows I want to change Image when User Click any two Images.

for Example I Click First Row 1 and Column 3 Image and Secondly I Click on Row 3 and Column 2 show now i want to change this two Images like Swap the Image How is it Possible ?

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    GridView gridView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridView = (GridView)findViewById(R.id.gridviewmy);
        gridView.setAdapter(new ImageAdapter(this));
        final ImageAdapter im = new ImageAdapter(this);
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                // TODO Auto-generated method stub
                int i=0; int j=0;
                if( i != 0){
                    j=arg2;
                    System.out.println("First Click "+j);
                }else{
                    i=arg2;
                    System.out.println("Second Click "+i);
                }
                im.getItem(arg2);
                //im.changeImage();
                Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
                System.out.println("AdapterView "+arg0);
                System.out.println("View "+arg1);
                System.out.println("Integer "+arg2);
                System.out.println("long "+arg3);
            }
        });
    }
}

class ImageAdapter extends BaseAdapter{
    private Context mContext;
    ImageView iView;
    public ImageAdapter(Context c){
        this.mContext = c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        System.out.println("Item Is :-"+mThumbIds[position].toString());
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        System.out.println("Geting Id of Item "+mThumbIds[position]);
        if(iView != null){
        iView.setImageResource(mThumbIds[0]);
        Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if( convertView == null){
            iView = new ImageView(mContext);
            iView.setLayoutParams(new GridView.LayoutParams(85, 85));
            iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iView.setPadding(8,8,8,8);
        }else{
            iView = (ImageView)convertView;
        }

            iView.setImageResource(mThumbIds[position]);
            return iView;
    }
    private Integer[] mThumbIds = {
            R.drawable.a_bhaibij,   R.drawable.a_dashera,       R.drawable.a_dipawali,
            R.drawable.a_gandhi,    R.drawable.a_holi,          R.drawable.a_indepe,
            R.drawable.a_janmastmi, R.drawable.a_kite,          R.drawable.a_newyear
            };
    public void changeImage(){
            iView.setImageResource(mThumbIds[5]);
    }
}
  • 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-05-22T22:19:44+00:00Added an answer on May 22, 2026 at 10:19 pm

    Swaping the images in the GridView is very simple.What you have to do is

    1* Store the cliked position,where you want to perform the swaping .

    2* By using those two values perform the swap operation on mThumbIds array.

    3* Finally invoke the notifyDataSetChanged() method on the Adapter object i.e im.notifyDataSetChanged();

    public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    int i=0;
    int firstClick,secondClick;
    GridView gridView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        gridView = (GridView)findViewById(R.id.gridviewmy);
        gridView.setAdapter(new ImageAdapter(this));
        final ImageAdapter im = new ImageAdapter(this);
        gridView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                // TODO Auto-generated method stub
                i++;
                if( i %2!=0){
                    firstClick=arg2;
                }else{
                    secondClick=arg2;
                    Integer help=new Interger(mThumbIds[firstClick]);
                    mThumbIds[firstClick]=mThumbIds[secondClick];
                    mThumbIds[secondClick]=help;
                    notifyDataSetChanged();
                    System.out.println("Second Click "+i);
                }
    
            }
        });
    }
    

    }

    class ImageAdapter extends BaseAdapter{
    private Context mContext;
    ImageView iView;
    public ImageAdapter(Context c){
        this.mContext = c;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }
    
    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        System.out.println("Item Is :-"+mThumbIds[position].toString());
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        System.out.println("Geting Id of Item "+mThumbIds[position]);
        if(iView != null){
        iView.setImageResource(mThumbIds[0]);
        Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
        }
        return 0;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
    
        if( convertView == null){
            iView = new ImageView(mContext);
            iView.setLayoutParams(new GridView.LayoutParams(85, 85));
            iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            iView.setPadding(8,8,8,8);
        }else{
            iView = (ImageView)convertView;
        }
    
            iView.setImageResource(mThumbIds[position]);
            return iView;
    }
    private Integer[] mThumbIds = {
            R.drawable.a_bhaibij,   R.drawable.a_dashera,       R.drawable.a_dipawali,
            R.drawable.a_gandhi,    R.drawable.a_holi,          R.drawable.a_indepe,
            R.drawable.a_janmastmi, R.drawable.a_kite,          R.drawable.a_newyear
            };
    

    }

    I think this may solve you problem.

    All the best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataBound GridView. However I have one column where the value comes
I have a GridView where one column is bound to an object property containing
I have a gridview that contains user info and one of the columns is
I have an Asp.Net page containing one GridView and a couple of images (google
I have a gridview that shows an image as part of one of its
I have a simple gridview that contains a label in one of the rows.
I have a gridview that displays items details, I added two template fields one
I have gridview, it's datasource is list<string> and I added one checkbox column to
I have two GridViews. One is Source GridView and another one is Destination GridView.
I have a gridview with an ObjectDataSource which I want to update with 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.