gpt4 book ai didi

java - 在android中运行时更改项目单击上的GridView图像

转载 作者:行者123 更新时间:2023-12-01 13:17:49 24 4
gpt4 key购买 nike

我在应用程序中使用GridView。我希望当我单击项目时,它应该替换在另一个数组图片上。实际上,当应用程序第一次运行时,我使用了两个数组,GridView 将显示第一个数组图片,之后当我单击项目时,它应该根据其位置替换为第二个数组图片。
我确实尝试过但没有解决。所以任何人都可以帮助我..

GridViewContent.java

public class GridViewContent extends BaseAdapter {
private Context context;

public int pictureArray[]={
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
};
public GridViewContent(Context c){
context=c;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureArray.length);
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureArray[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView myimage=new ImageView(context);
myimage.setImageResource(pictureArray[position]);
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));


return myimage;
}

}

MainActivity.java

public class MainActivity extends Activity {
Context ctx;
GridViewContent grid1 = new GridViewContent(ctx);
// GridView grid;
/*
* static final int[] numbers = new int[] {
* R.drawable.question,R.drawable.question
* ,R.drawable.question,R.drawable.question
* ,R.drawable.question,R.drawable.question, R.drawable.question,
*
* };
*/
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView grid = (GridView) findViewById(R.id.gv_memory);
grid.setAdapter(new GridViewContent(this));
// grid.setOnItemClickListener(itemClickListener);
grid.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
ImageView pictImage = new ImageView(ctx);
int imgView = OriginalArray[id];
pictImage.setImageResource(imgView);

}
});

}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gv_memory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >

</GridView>

提前致谢......

最佳答案

您需要做一些小的更改,如下所示:

public class GridViewContent extends BaseAdapter {
private Context context;

public int pictureArray[]={
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
R.drawable.question,
};

public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4,
R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, };

public GridViewContent(Context c){
context=c;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureArray.length);
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureArray[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView myimage=new ImageView(context);
myimage.setImageResource(pictureArray[position]);
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));




myimage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
myimage.setImageResources(Original[arg2]);
}
});
return myimage;
}

}

关于java - 在android中运行时更改项目单击上的GridView图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22323325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com