我在第一个活动中使用带有毕加索的字符串数组URL在gridview中显示随机图像,当我在gridview中单击任何图像时,我想在下一个活动中显示确切的随机图像。我正在使用put extra并像int r = random.nextInt(array.length);
那样发送该位置,我正在r中使用r到gridview中,把extra作为位置。但是当我在imageview中设置r时,它显示的是另一幅不精确的随机图像。
这是我的代码
public class MainActivity extends Activity {
static Random random;
private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
static int p;
// Some items to add to the GRID
static final String[] icons= {
"https://abhiandroid.com/ui/wp-content/uploads/2015/12/horizontalSpacing-in-Gridview.jpg",
"http://www.whatsappstatusmessages.com/wp-content/uploads/2017/01/whatsapp-dp-images-in-english.jpg",
"http://www.sarkarinaukrisearch.in/wp-content/uploads/2019/02/whatsapp-dp-status-in-english-1-77.jpg",
"https://www.trueshayari.in/wp-content/uploads/2018/07/Love-Status-DP-for-Couple.jpg",
"https://4.bp.blogspot.com/-2iawNx83Kpw/XL21pPj0aPI/AAAAAAAAKiE/VRR7pupbWDUj0TNNAKdGH8Baaz_c9IcSgCLcBGAs/s1600/ss.jpg",
"https://www.trueshayari.in/wp-content/uploads/2018/07/Love-Status-DP-for-Couple.jpg",
"https://3.bp.blogspot.com/-8us6YRiZEh0/XL21c6ibbXI/AAAAAAAAKh4/eNyjErq7q04YCeWxDPWojYfOoAC8BCodwCLcBGAs/s1600/s.jpg"
};
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.albumGrid);
CustomAdopter customAdopter=new CustomAdopter();
gridView.setAdapter(customAdopter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SwipeView.class);
intent.putExtra("id", p);
startActivity(intent);
}
});
}
private static class CustomAdopter extends BaseAdapter {
static int p;
@Override
public int getCount() {
return icons.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.phototem, null);
ImageView imageView = view.findViewById(R.id.cover);
random = new Random(); // or create a static random field...
p= random.nextInt(icons.length);
Picasso.get().load(icons[p]).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
return view;
}
}
在此活动中显示图像
public class SwipeView extends Activity
{
int positions;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe_view);
// get intent data
Intent i = getIntent();
// Selected image id
positions = i.getExtras().getInt("id");
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(technoapps4.goodnightshayariworld.MainActivity.p);
}
private class ImagePagerAdapter extends PagerAdapter
{
String[] icons =MainActivity.icons ;
@Override
public int getCount()
{
return icons.length;
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position)
{
Context context = SwipeView.this;
ImageView imageView = new ImageView(context);
Picasso.get().load(icons[position]).into(imageView);
container.addView(imageView, 0);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object)
{
container.removeView((ImageView) object);
}
}
}
答案 0 :(得分:1)
为此,您应该创建一个Interface
并通过该接口传递随机位置。
并且变量p
不是单击的项目位置。 p
的值在gridView
填充项的同时不断变化。
编辑:
像这样创建一个Interface
:
public interface ItemClickListener {
void onItemClick(int position);
}
现在使用公共设置器初始化ItemClickListener
类中的CustomAdopter
实例。
public void setItemClickListener(ItemClickListener clickListener) {
onItemClickListener = clickListener;
}
最后,在getview
方法内添加以下代码,以将适配器位置传递给侦听器。
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.onItemClick(p);
}
});
现在,使用该界面来获取正确的位置,而不是GridView
的{{1}}。像这样-
OnItemClickListener()
答案 1 :(得分:0)
我将为您提供此问题的替代方法,即使用带有gridLayoutManager的recyclerview并创建实现RecyclerView.Adapter的自定义布局。...我想所有这些似乎都使您感到困惑,所以我很快创建一个单元测试来模拟您的代码想要实现的目标,然后是结果。
public class ExampleUnitTest {
private static String data = null;
@Test
public void setParentData(){
data = "I set You";
Subclass subclass = new Subclass();
subclass.getParentData();
}
private static class Subclass{
void getParentData(){
assertEquals("I set You", data);
}
}
}
//Tests passed
由于您将自定义适配器作为MainActivity的子类,所以我将花一分钟的时间对此进行解释,然后我相信该子类应该可以访问其父类的某些值。
我在说的是:static int p;
,所以您无需像在子类(CustomAdopter类)中创建的那样创建其中的两个。
因此,下面是对您的代码的修改,我认为它应该在测试通过后才能起作用,否则,请让我知道,以便我再次查看。
这并不是真正的答案,但这是我共享代码并详细解释一些事情的唯一方法。
public class MainActivity extends Activity {
static Random random;
private GridView photoGrid;
private int mPhotoSize, mPhotoSpacing;
static int p;
// Some items to add to the GRID
static final String[] icons= {
"https://abhiandroid.com/ui/wp-content/uploads/2015/12/horizontalSpacing-in-Gridview.jpg",
"http://www.whatsappstatusmessages.com/wp-content/uploads/2017/01/whatsapp-dp-images-in-english.jpg",
"http://www.sarkarinaukrisearch.in/wp-content/uploads/2019/02/whatsapp-dp-status-in-english-1-77.jpg",
"https://www.trueshayari.in/wp-content/uploads/2018/07/Love-Status-DP-for-Couple.jpg",
"https://4.bp.blogspot.com/-2iawNx83Kpw/XL21pPj0aPI/AAAAAAAAKiE/VRR7pupbWDUj0TNNAKdGH8Baaz_c9IcSgCLcBGAs/s1600/ss.jpg",
"https://www.trueshayari.in/wp-content/uploads/2018/07/Love-Status-DP-for-Couple.jpg",
"https://3.bp.blogspot.com/-8us6YRiZEh0/XL21c6ibbXI/AAAAAAAAKh4/eNyjErq7q04YCeWxDPWojYfOoAC8BCodwCLcBGAs/s1600/s.jpg"
};
GridView gridView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.albumGrid);
random = new Random(); // or create a static random field...
p= random.nextInt(icons.length);
CustomAdopter customAdopter=new CustomAdopter();
gridView.setAdapter(customAdopter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, SwipeView.class);
intent.putExtra("id", p);
startActivity(intent);
}
});
}
private static class CustomAdopter extends BaseAdapter {
@Override
public int getCount() {
return icons.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.phototem, null);
ImageView imageView = view.findViewById(R.id.cover);
Picasso.get().load(icons[p]).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(imageView);
return view;
}
}