Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
733 views
in Technique[技术] by (71.8m points)

android - How can I change Image on Gridview Runtime?

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]);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...