We Rub Chan
We Rub Chan

Reputation: 205

Change data in listview at fragment

I have create a listview in a fragment in a tab in Android 2.3. I want to change the data in listview when I click the tab. For example, in "tab1" I can see "1,2,3" in listview, then I click to "tab2" and click back to "tab1", the listview change to show "a,b,c". I have create a refresh() method, and call it in onPageSelected, but it always got NullPointerException.

Here is my code:

List_View

public class List_View extends ListFragment{

    protected ArrayAdapter listAdapter;
    private ArrayList<String> items = new ArrayList<String>();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.list, container, false);
        items.add("1");
        items.add("2");
        items.add("3");

        listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
        setListAdapter(listAdapter);

        return rootView;
    }

    public void refresh(){
        items.clear();
        listAdapter.notifyDataSetChanged();
        items.add("a");
        items.add("b");
        items.add("c");
        listAdapter.notifyDataSetChanged();
    }

}

TabsAdapter

public class TabsAdapter extends FragmentStatePagerAdapter implements TabListener, OnPageChangeListener{
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    private final String TAG = "";
    private List_View list_view;

    static final class TabInfo{
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args){
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = bar;
        mViewPager = pager;
        mViewPager.setOffscreenPageLimit(0);
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(Tab tab, Class<?> clss, Bundle args){
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);

        list_view.refresh();
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
        Log.v(TAG, "clicked");
        Object tag = tab.getTag();
        for (int i = 0; i<mTabs.size(); i++){
            if (mTabs.get(i) == tag){
                mViewPager.setCurrentItem(i);
            }
        }
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(), info.args);
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

}

Upvotes: 0

Views: 686

Answers (1)

Raghunandan
Raghunandan

Reputation: 133580

I have create a refresh() method, and call it in onPageSelected, but it always got NullPointerException

You have

private List_View list_view;

And you have

list_view.refresh();

But i don't see any code that initialized list_view.

Fragment to Fragment communication is done through the associated Activity.

http://developer.android.com/training/basics/fragments/communicating.html

So use a interface as a callback to the activity from fragment in tab2 and then communicate to List_View and change the list accordingly.

Upvotes: 1

Related Questions