所以我在 ViewPager 中有一个 ListView。
我有ListView
一个布局(add_site.xml)
和一个EditText
将填充ListView
onClick
另一个布局的字段(main.xml)
我收到一个 NPE 错误,因为我试图将 ArrayAdapter 全局化,认为这是能够在ViewPager
.
显然我的 android 开发技能不是那么好。我对此很陌生。
所以任何帮助表示赞赏!
这是我的代码。
这是 PagerAdapter 开始的地方
private class MyPagerAdapter extends PagerAdapter {
final ViewPager mPager = (ViewPager) findViewById(R.id.fieldspager);
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ListView lv = (ListView)findViewById(android.R.id.list);
final ArrayList<String> siteList = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(getBaseContext(), R.layout.list_row, siteList);
lv.setAdapter(aa);
int resId = 0;
View view = null;
switch (position) {
case 0:
resId = R.layout.field01;
view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
case 1: //ListView lives in here.
resId = R.layout.add_site;
view = inflater.inflate(resId, null);
Button addSiteButton = (Button)view.findViewById(R.id.addSiteButton);
((ViewPager) collection).addView(view, 0);
addSiteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPager.setCurrentItem(2, true);
}
});
//I originally had the ArrayAdapter code here, but thought it needed to be global so I moved it up.
return view;
case 2:
resId = R.layout.main;
view = inflater.inflate(resId, null);
Button signInButton = (Button)view.findViewById(R.id.sign_in_button);
//This is where the EditText lives
final EditText editText = (EditText)view.findViewById(R.id.textViewId);
((ViewPager) collection).addView(view, 0);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//This is where the button click happens
siteList.add(0, editText.getText().toString());
aa.notifyDataSetChanged();
editText.setText("");
mPager.setCurrentItem(1, true);
}
});
return view;
}
return view;
}
先谢谢了!