我基本上想在每种情况下都启动新的Activity,但这是问题所在:我希望在“ if语句”为true时启动新的Activity。因此,当用户到达柜台的某个位置(假设2)时,预期的活动应开始。预先感谢!
以下是一些代码:
public class Adapter extends PagerAdapter {
private List<Model>models;
private LayoutInflater layoutInflater;
private Context context;
public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
}
@Override
public int getCount() {
return models.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
layoutInflater=LayoutInflater.from(context);
View view =layoutInflater.inflate(R.layout.item, container, false);
ImageView imageView;
TextView title, desc;
imageView=view.findViewById(R.id.image);
title=view.findViewById(R.id.title);
desc=view.findViewById(R.id.desc);
imageView.setImageResource(models.get(position).getImage());
title.setText(models.get(position).getTitle());
desc.setText(models.get(position).getDesc());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//HERE IS MY PROBLEM
if(position == 0){
//start activity1
} else if(position == 1){
//start activity2
}else if(position == 2){
//start activity3
}else if(position == 3){
//start activity4
}else {
Toast.makeText(context, "Click the exit button", Toast.LENGTH_SHORT).show();
}
}
});
container.addView(view,0);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
}
答案 0 :(得分:0)
此代码运行良好:
view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if(position == 0){
Intent i = new Intent(view.getContext(),Levels.class);
view.getContext().startActivity(i);
}