如何在android中动态添加listview中的项目?

时间:2013-09-12 11:42:34

标签: java android listview android-listview

嗨我知道这样的问题好几次,但我找不到办法解决我的问题 我想做什么:我想在刷卡页面项目中从我的mainactivity 向listview添加项目

我的主要活动

public class MainActivity extends SherlockFragmentActivity {
// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);
    final Context context = getApplicationContext();

    for (int s = 0; s < 10; s++) {

        Swap.stafu1.add("F" + s);
        Swap.stafu2.add("F" + s);
    }

    // Activate Navigation Mode Tabs
    mActionBar = getSupportActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Locate ViewPager in activity_main.xml
    mPager = (ViewPager) findViewById(R.id.pager);

    // Activate Fragment Manager
    FragmentManager fm = getSupportFragmentManager();

    // Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            mActionBar.setSelectedNavigationItem(position);

            if (position == 1) {
                Button b = (Button) findViewById(R.id.button1);
                b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Log.d("bingo", "clicked" + Swap.stafu1.size());
                        Swap.stafu1.add("sometext");
                        Swap.stafu2.add("sometext");

                        Swap.stafu1.remove(0);
                        Swap.stafu2.remove(0);

                        FragmentTab2 f2 = new FragmentTab2();
                        f2.nDChanged();

                    }
                });

            }

        }

    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    mPager.setAdapter(viewpageradapter);

    // Capture tab button clicks
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // Pass the position on tab click to ViewPager
            mPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    };

    // Create first Tab
    tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create second Tab
    tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create third Tab
    tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
    mActionBar.addTab(tab);

}

}

FragmentTab2.java

 public class FragmentTab2 extends SherlockFragment {
ListView list;
ListViewAdapter adapter;

@Override
public SherlockFragmentActivity getSherlockActivity() {
    return super.getSherlockActivity();
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragmenttab2, container,
            false);
    // Locate the ListView in fragmenttab1.xml
    list = (ListView) rootView.findViewById(R.id.listview);

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(getActivity(), Swap.stafu1, Swap.stafu2);
    // Binds the Adapter to the ListView
    list.setAdapter(adapter);
    return rootView;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    setUserVisibleHint(true);
}

public void nDChanged() {
    Log.d("bingo", "clicked"+Swap.stafu1.size());
    adapter = new ListViewAdapter(getActivity(), Swap.stafu1, Swap.stafu2);
    adapter.notifyDataSetChanged();
}

}

Swap.java

public class Swap {
public static ArrayList<String> stafu1=new ArrayList<String>();
public static ArrayList<String> stafu2=new ArrayList<String>();
}

问题: notifyDataSetChanged()直到我从listview中删除一个项目才开始工作,这意味着我无法在listview中增加项目。
当我不使用notifyDataSetChanged()时,我可以向Swap.stafu1和Swap.stafu1添加文本,当我滑动页面并将页面滑动回fragmenttab2时,它会出现在listview中。
我想我每次在listview上添加项目时都需要刷新listview或fragmenttab2。但我不知道我该怎么做。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您需要在listview适配器中放置此notifyDataSetChanged()和registerDataSetObserver()方法。

private ArrayList<DataSetObserver> observers = new ArrayList<DataSetObserver>();

public void registerDataSetObserver(DataSetObserver observer) {
    observers.add(observer);
}
public void notifyDataSetChanged(){
    for (DataSetObserver observer: observers) {
        observer.onChanged();
    }
}