当我想在这个类中使用getApplication()
时遇到问题,错误是需要使用...我应该使用什么代替getApplication()
(Becaus我想使用{{1}的方法1}}被命名为TestClass
)或我应该如何setNamePermit
测试类的方法。
setNamePermit()
测试类是:
public class CustomSwipeAdapter01 extends PagerAdapter{
private int[] image_Resources = {R.drawable.sample_01,R.drawable.sample_02,R.drawable.sample_03,R.drawable.sample_04,R.drawable.sample_05,R.drawable.sample_06,R.drawable.sample_07};
private Context ctx;
private LayoutInflater layoutInflater;
public TestClass app;
public CustomSwipeAdapter01(Context ctx) {
this.ctx = ctx;
}
@Override
public int getCount() {
return image_Resources.length;
}
@Override
public boolean isViewFromObject(View view, Object o) {
return (view == (RelativeLayout) o);
}
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View item_view=layoutInflater.inflate(R.layout.activity_story01,container,false);
ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
TextView textView=(TextView)item_view.findViewById(R.id.image_count);
Button btn_back_story01 = (Button) item_view.findViewById(R.id.btn_back_story01);
imageView.setImageResource(image_Resources[position]);
int itemNo=position+1;
textView.setText(itemNo + "/" + getCount());
container.addView(item_view);
//what should use instead of getApplication() in below line:
app = (TestClass)getApplication();
btn_back_story01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((Activity) ctx).finish();
app.setNewPermit(false);
ctx.startActivity(new Intent(ctx, MainStory01.class));
}
});
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
}
答案 0 :(得分:1)
TestClass tc = new TestClass();
访问TestClass中的方法:
tc.setNewPermit(false);
您的寻呼机适配器中的 更新:,您现在可以传递任何这些值。例如,将btn_back_story01
onClick()
更改为:
btn_back_story01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CustomSwipeAdapter01.this, MainStory01.class);
intent.putExtra("is_new_permit", tc.getNewPermit());
startActivity(intent);
}
});
在MainStory01
活动的onCreate()
中,您现在可以通过Intent
获取Bundle
中传递的额外内容...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
boolean isNewPermit = bundle.getBoolean("is_new_permit");
}
}
代码中可能存在一些错误,我目前不在我的工作计算机上,但这应该让您了解如何继续。