我有一个视图寻呼机应用程序正常工作。视图分页器在滑动后一次显示一个数组中包含不同的数据字符串。我有一个按钮,点击后应获取当前内容并将其传递给另一个活动,这是我的代码:
/*MainActivity*/
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
setContentView(R.layout.activity_main);
btn = (Button) findViewById (R.id.button1);
item = new String[] { "Apple", "Movie", "Melon", "pawpaw",
"Jungle", "Forest" };
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ViewPagerAdapter(this, item);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), ContentActivity.class);
i.putExtra("Currentitem", item[0].toString());
startActivity(i);
}
});
}
以下是ViewPagerAdapter类
public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] item;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] item) {
this.context = context;
this.quote = item;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView item;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.activity_view_pager_adapter, container,
false);
txtitem = (TextView) itemView.findViewById(R.id.quote);
txtitem.setText(quote[position]);
((ViewPager) container).addView(itemView);
return itemView;
提前致谢
答案 0 :(得分:0)
这应该可以正常工作:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int currentItem =viewPager.getCurrentItem();
Intent i = new Intent(getApplicationContext(), ContentActivity.class);
i.putExtra("Currentitem", item[currentItem].toString());
startActivity(i);
}
});
祝福。