当我从listview中添加或删除一个项目时,listview不会更新,除非我在viewpager中滚动到屏幕外,然后返回,在片段中再次触发OnCreate方法。这是基于eclipse中的viewpager模板的,我只是在片段中添加了一个listview,而不仅仅是一个textview。将notifyDataSetChanged放置到片段中没有帮助,也没有向片段中添加OnResume。以下是主要活动。adapter和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方法中的notifyDataSetChanged<当您滚动查看列表视图时,会调用code>onCreateView,然后在屏幕上更新数据。更改适配器中的数据后,需要立即调用notifyDataSetChanged,而不是在onCreateView中。
02-27 04:45:26.808:e/AndroidRuntime(4739):致命异常:main 02-27 04:45:26.808:e/AndroidRuntime(4739):java.lang.runtimeException:无法启动活动ComponentInfo{com.koenb_fashion_fix/com.koenb_fashion_fix.mainActivity}:j
在我的android应用程序中,两个月以来我一直无法解决这个问题,即使在尝试了堆栈溢出中发现的许多类似解决方案之后。我正在用ArrayAdapter填充listview。长时间按下listview的某个项目时,会出现一个对话框,要求确认。确认后,所选项目将从数组列表中删除,但在listview中,最后一个项目似乎已删除。当您关闭片段并返回到它时,您会看到正确的项目,但不会在关闭警报对话框后立即看到
我正在尝试将ListView与fragmnet中的自定义适配器(baseAdapter)一起使用。 当我直接在MainActivity中使用此代码时,一切正常,但当我在片段中使用此代码时,它没有崩溃,但它没有显示任何内容,它只是一个空白片段。另外,当我尝试使用简单的arrayAdapter在片段中绑定一个textView时,它工作得很好,所以我认为问题将出现在我的自定义适配器中。 为什么不显示Li
我有Mainactive,它保存片段。我的一个片段(参与者)检查数据库中是否有任何内容。如果没有,则显示带有消息以添加数据的片段(空参与者列表)。如果是,它显示带有2个选项卡的TabHost片段,其中一个包含带有数据库条目的ListView片段(参与者列表)。有一个FAB按钮可以添加更多记录。添加另一条记录后如何刷新ListView?我不确定,因为TabHost不适用于我在应用程序中使用的Frag
问题内容: 我正在尝试制作一个包含名称的列表。该列表应该是可修改的(添加,删除,排序等)。但是,每当我尝试更改ArrayAdapter中的项目时,程序都会崩溃,并显示错误。这是我的代码: 我尝试插入,删除和清除方法,但没有一个起作用。有人可以告诉我我做错了什么吗? 问题答案: 我自己尝试了一下…发现它没有用。所以我检查了ArrayAdapter的源代码并发现了问题。由数组初始化后,将其转换为不能修
我的问题是,当更改时,我的(复选框列表视图)不会更新。 只有当我使用有日期的JavaFX版本和有日期的JRE(jre6和JFX2.2)时,才会出现这个问题。当我使用jre8和它所包含的JavaFX时,问题就没有了,列表也会很好地刷新。 上面描述的最新JavaFX上的工作源代码: 名单: 列表使用的类具有以下属性: 问题是,当我更改列表项时,ListView在旧的JavaFX版本上不会改变。 值得一