gpt4 book ai didi

java - PagerAdapter 中的 ImageButton onclick。需要转到另一个类/XML 文件。错误

转载 作者:行者123 更新时间:2023-12-02 04:26:22 25 4
gpt4 key购买 nike

对此进行了一段时间的研究,但似乎找不到任何解决方案。我有 5 张采用 PagerLayout 滑动格式的图像。我想要做的是单击图像后,我想要转到另一个 XML 文件/类。

代码:(我已经注释掉了我尝试过的方法。)

public class CustomSwipeAdapter extends PagerAdapter {
Context context;
private int[] image_resources = {R.drawable.chestpress,R.drawable.deadlift,R.drawable.squat,R.drawable.pullups,R.drawable.dips};
private Context ctx; //Gets the context of a current state or application. Tells program what is going on somewhere else.
private LayoutInflater layoutInflater; //USed to instantiate layout XML files to View Objct.
private LayoutInflater inflater;
private String[] names = {"Dumbell Press","Deadlift","Squats","Pullups","Tricep Dips"};
public ImageView imageView;
Activity activity;
View views = null;


public CustomSwipeAdapter(Context ctx){
this.ctx = ctx;
}


@Override
public int getCount() {
return image_resources.length;
}

@Override
public boolean isViewFromObject(View view, Object o) {
return view==(LinearLayout)o;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
imageView = (ImageView)item_view.findViewById(R.id.image_view);
TextView textView = (TextView)item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText(names[position]);
container.addView(item_view);


imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position == 0) {
Log.d("Exercise","Dumbell Press");
//Intent intent = new Intent(ctx,MyActivity.class);
//Intent intent = new Intent(CustomSwipeAdapter.this,MyActivity.class);
//Intent intent = new Intent(context,MyActivity.class);
//views = inflater.inflate(R.layout.main,null);
//context.startActivity(intent);
//MyActivity koo = new MyActivity(); (Tried just instantiating another class (Im so lost)

}
if(position == 1){
Log.d("Exercise","Deadlift!");
}
if(position == 2){
Log.d("Exercise","Squats");
}
}
});

return item_view;
}




@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}

非常新的 android/java 程序员。感谢您的阅读。

最佳答案

试试这个:

Intent intent = new Intent(CustomSwipeAdapter.this.ctx, MyActivity.class);
CustomSwipeAdapter.this.ctx.startActivity(intent);

还要确保将正确的上下文传递给您的适配器构造函数。您将传递的上下文可以是 Activity 本身。

因此,在您的 Activity 上,当您需要创建新适配器时,请执行以下操作:

CustomSwipeAdapter myAdapter = new CustomSwipeAdapter(this);

其中 this 是您要在其上创建适配器的 Activity 。如果您需要在 fragment 内创建适配器,只需使用以下命令获取 Activity :

MyFragment.getActivity();

最后,如果您想知道:“为什么这有效?它需要一个上下文,而我正在传递一个 Activity !”。

这是一个简短的答案:

Activity inherits context. Thus, if you are in an activity, you only need to pass itself to use the context. It also contains a pointer to getBaseContext(). You might occasionally need to reference that, if you need the entire application context, but most likely you won't for a while.

希望这有帮助。如果您遇到更多错误,请告诉我(不要忘记发布您的 logcat,以便我们可以更好地帮助您)。

关于java - PagerAdapter 中的 ImageButton onclick。需要转到另一个类/XML 文件。错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103199/

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