gpt4 book ai didi

android - fragment 内带有 ListView 的 ViewPager - ListView 在修改时不更新

转载 作者:行者123 更新时间:2023-11-29 00:18:07 25 4
gpt4 key购买 nike

当我在 ListView 中添加或删除项目时, ListView 不会更新,除非我在 viewpager 中滚动到屏幕外然后返回,在 fragment 中再次触发 OnCreate 方法。这是基于 eclipse 中的 viewpager 模板——我只是向 fragment 添加了一个 ListView ,而不仅仅是一个 TextView 。放置 notifyDataSetChanged 无济于事,也无助于向 fragment 添加 OnResume。以下是主要 Activity 。适配器和 ListView 中有正确的项目,但直到您滚动至少两页(在我的设备上不在屏幕上,在其他设备上可能或多或少)然后返回,它们才会显示。我错过了什么?

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;
}

}

最佳答案

您正在 onCreateView 方法中调用 notifyDataSetChangedonCreateView 在您滚动 ListView 时调用,然后屏幕上的数据会更新。在适配器中更改数据后,您需要立即调用 notifyDataSetChanged,而不是在 onCreateView 中调用。

关于android - fragment 内带有 ListView 的 ViewPager - ListView 在修改时不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25006388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com