Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在一个活动中更新同一片段的两个实例_Android_Android Fragments_Android Listview - Fatal编程技术网

Android:在一个活动中更新同一片段的两个实例

Android:在一个活动中更新同一片段的两个实例,android,android-fragments,android-listview,Android,Android Fragments,Android Listview,我有一个带有一个tabbar和两个片段的活动。这些片段是同一片段类的不同实例。两个片段碰巧都有一个listview 在活动的后台进程中加载新数据后,我想更新这两个片段中的两个ListView。这基本上是我的更新代码: // Code inside an AsynTask inside my MainActivity that gets called after I load some data from the server final FragmentTransaction ft = getS

我有一个带有一个tabbar和两个片段的活动。这些片段是同一片段类的不同实例。两个片段碰巧都有一个listview

在活动的后台进程中加载新数据后,我想更新这两个片段中的两个ListView。这基本上是我的更新代码:

// Code inside an AsynTask inside my MainActivity that gets called after I load some data from the server
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(MainActivity.this.fragment1);
ft.attach(MainActivity.this.fragment1);
ft.commit();

final FragmentTransaction ft2 = getSupportFragmentManager().beginTransaction();
ft2.detach(MainActivity.this.fragment2);
ft2.attach(MainActivity.this.fragment2);
ft2.commit();
但实际情况是,这两个片段在这次更新后显示相同的listview项,并且它们始终显示上次更新片段的项

// onCreate method in my MainActivity
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //... initiation of action bar and sectionspager adapter here....

        // I create two different instances of "MyFragment" and I pass an integer (=status) to the constructor
        this.fragment1= new MyFragment(1);
        this.fragment2 = new MyFragment(2);
}
标题和listview项应取决于在构造函数中传递的整数。但是,只有两个片段的标题不同,而不是两个ListView中的项目

// inside the onCreate Method of my Fragment class
// Based on the passed integer I show a different title and I get a different list of items
if (status == 1)
    tvTitle.setText("Title 1");
} else if (status == 2){
    tvTitle.setText("Title 2");
}

ListView lv = (ListView) rootView.findViewById(R.id.fragment_list_view);

ArrayList<Joboffer> itemList = DataHolder.getInstance().getItemListForStatus(this.status);
lv.setAdapter(new ItemAdapter(getActivity(), itemList ));
//在我的片段类的onCreate方法中
//根据传递的整数,我显示不同的标题,并得到不同的项目列表
如果(状态==1)
tvTitle.setText(“标题1”);
}否则如果(状态==2){
tvTitle.setText(“标题2”);
}
ListView lv=(ListView)rootView.findViewById(R.id.fragment\u list\u视图);
ArrayList itemList=DataHolder.getInstance().getItemListForStatus(this.status);
setAdapter(新的ItemAdapter(getActivity(),itemList));
我不确定我的问题有多普遍,但也许我对ListView的工作原理缺乏一些基本的了解。两个ListView都应该是不同的实例,在两个不同的片段中,使用两个不同的适配器。但有些人认为这是行不通的