因此,在单击另一个片段中的按钮后,在 EditText 字段中添加文本后,我尝试在一个片段中填充 ListView。
我的 Java 编码技能很差,所以任何代码建议都会对我有很大帮助。
我希望这是有道理的。
这是我的代码。
public class FieldsActivity extends FragmentActivity {
private static final int NUMBER_OF_PAGES = 3;
ViewPager mPager;
MyFragmentPagerAdapter mMyFragmentPagerAdapter;
ArrayList<String>siteList = new ArrayList<String>();
CustomAdapter ca = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.fields);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.topbar);
mPager = (ViewPager) findViewById(R.id.fieldspager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mMyFragmentPagerAdapter);
mPager.setCurrentItem(1);
}
我的 ListView 在哪里。
public class AddSite extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.add_site, container, false);
Button addSiteButton = (Button) mRelativeLayout.findViewById(R.id.addSiteButton);
addSiteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPager.setCurrentItem(2, true);
}
});
ListView lv = (ListView) findViewById(android.R.id.list);
CustomAdapter ca = new CustomAdapter();
lv.setAdapter(ca);
return mRelativeLayout;
}
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter() {
super(FieldsActivity.this, android.R.layout.simple_list_item_1, siteList);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.list_row, null);
}
((TextView)row.findViewById(R.id.textViewId)).setText(siteList.get(position));
}
return row;
}
}
Button 和 EditText 在哪里。
public class CreateSite extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.main, container, false);
final EditText siteEditText = (EditText)findViewById(R.id.siteEditText);
Button signInButton = (Button) mRelativeLayout.findViewById(R.id.sign_in_button);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //Click this button and add to ListView in class AddSite
siteList.add(0, siteEditText.getText().toString());
ca.notifyDataSetChanged(); //(Bundle) cannot be applied
siteEditText.setText("");
mPager.setCurrentItem(1, true);
}
});
return mRelativeLayout;
}
}
虽然都在 FragmentPagerAdapter
private class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return new SettingFields();
case 1: return new AddSite();
case 2: return new CreateSite();
}
return null;
}
@Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
}
所以我评论了显示的错误。
希望这是有道理的。如果您需要查看其他内容,请告诉我。
提前致谢!