したがって、ViewPager 内に ListView があります。
あるListView
レイアウト(add_site.xml)
と、別のレイアウトにEditText
データを入力するフィールドがありますListView
onClick
(main.xml)
ArrayAdapter を globlaly に配置しようとしたため、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;
}
ありがとうございます!