当我在列表视图中添加或删除项目时,列表视图不会更新,除非我在viewpager中滚动屏幕然后再返回,在片段中再次触发OnCreate方法。这基于eclipse中的viewpager模板 - 我只是在片段中添加了一个listview而不仅仅是textview。放置notifyDataSetChanged也没有帮助,也没有向片段添加OnResume。以下是主要活动。适配器和列表视图中包含正确的项目,但是直到您滚动至少两页(在我的设备上屏幕外,可能或多或少在其他页面上)然后返回时它们才会显示。我错过了什么?
public class MainActivity extends ActionBarActivity implements ActionBar.TabListener {
SectionsPagerAdapter mSectionsPagerAdapter;
static ArrayAdapter adapter;
private static DataElementTable dataElementTable;
private static ItemTable ItemTable;
private static int currentTab = 0;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
dataElementTable = new DataElementTable(this);
dataElementTable.open();
itemTable = new ItemTable(this);
itemTable.open();
}
@Override
protected void onResume() {
dataElementTable.open();
itemTable.open();
super.onResume();
}
@Override
protected void onPause() {
dataElementTable.close();
itemTable.close();
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
addNewItem();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
private void addNewItem()
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("New Item");
alert.setMessage("Enter New Item Name:");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
Log.d("pos", "newitem " + currentTab);
Item newItem = itemTable.createItem(currentTab - 1, value);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class
// below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 7 total pages.
return 7;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
case 5:
return getString(R.string.title_section6).toUpperCase(l);
case 6:
return getString(R.string.title_sectionX).toUpperCase(l);
}
return null;
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
ListView listView;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber)
{
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textView = (TextView) rootView
.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
currentTab = (getArguments().getInt(ARG_SECTION_NUMBER)) - 1;
listView = (ListView) rootView.findViewById(R.id.listview);
ArrayList<String> listItems1 = new ArrayList<String>();
ArrayList<String> listItems2 = new ArrayList<String>();
ArrayList<String> listItems3 = new ArrayList<String>();
ArrayList<String> listItems4 = new ArrayList<String>();
ArrayList<String> listItems5 = new ArrayList<String>();
ArrayList<String> listItems6 = new ArrayList<String>();
ArrayList<String> listItemsLog = new ArrayList<String>();
listItems1 = itemTable.getAllItemsForCategory(0);
listItems2 = itemTable.getAllItemsForCategory(1);
listItems3 = itemTable.getAllItemsForCategory(2);
listItems4 = itemTable.getAllItemsForCategory(3);
listItems5 = itemTable.getAllItemsForCategory(4);
listItems6 = itemTable.getAllItemsForCategory(5);
Log.d("pos", "createview " + currentTab);
switch (currentTab)
{
case 0:
{
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItems1);
}
break;
case 1:
{
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItems2);
}
break;
case 2:
{
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItems3);
}
break;
case 3:
{
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItems4);
}
break;
case 4:
{
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItems5);
}
break;
case 5:
{
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItems6);
}
break;
case 6:
{
listItemsLog = dataElementTable.getAllElements();
adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.list_item, listItemsLog);
}
break;
default:
{
assert(false);
}
break;
}
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
return rootView;
}
}
}
答案 0 :(得分:2)
您正在使用notifyDataSetChanged
方法调用onCreateView
。滚动浏览onCreateView
时会调用ListView
,然后屏幕上的数据会更新。更改适配器中的数据后,您需要立即致电notifyDataSetChanged
,而不是onCreateView
。