如何从另一个fagment选项卡刷新listview

时间:2016-03-01 15:08:49

标签: android listview tabs

我有3个片段的Tabs,在第一个标签中我有注册表单,在第二个标签中将数据保存到sqlite我有从该数据库中提取数据的列表并在简单列表视图中显示它,所以如何在点击保存按钮时进行在第一个选项卡中,第二个选项卡列表视图应自动更新,而不重新启动活动

View v = inflater.inflate(R.layout.tab_2,container,false);
        mListView = (ListView) v.findViewById(R.id.lvMain);
        DataBaseHandler db = new DataBaseHandler(getActivity());

        List<Contact> contacts = db.getAllContacts();
        List<String> your_array_list = new ArrayList<String>();

        for (Contact cn : contacts) {
            String log = "ID:" + cn.get_id() + " Name: " + cn.get_name()
                    + cn.get_cena()+" "+cn.getJob()+" "+cn.get_opis();
            your_array_list.add(cn.get_name());
            // Writing Contacts to log
            Log.d("77777777777777: ", log);
            //add contacts data in arrayList
            //imageArry.add(cn);

        }

       // your_array_list.add("bar");
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                your_array_list );
        mListView.setAdapter(arrayAdapter);

1 个答案:

答案 0 :(得分:1)

检查此解决方案。我想也适合你。 How do I update ListView in another Fragment?

@Override
public void onPageSelected(int position) {
ListViewFragment frag=(ListViewFragment)adapter.instantiateItem(viewPager, 1);
//here adapter is the ViewPager Adapter and you must supply the viewpager that    contains 
//the fragments and also the position of the fragment to instantiate. 
//For example 0 or 1 etc.
frag.populateListView(getApplicationContext());
aBar.setSelectedNavigationItem(position);
}