我的问题是,当我从我的另一个片段输入数据时,我无法更新我的AcTview适配器的数据。
在PageAdjuFragment
我有ACTV tv2
和他的适配器adapter2
。
此适配器的数据由getListpart()
中的VenteFragment
创建(PageAdjuFragment extends VenteFragment)。
我知道当我重新启动应用程序时,getListpart()和initpart()会起作用,新数据会出现在我的actv中。
我认为问题是当我在片段之间切换时,它们已经启动,所以数据在开始时只被初始化一次。
当我在我的应用上切换页面时,我在片段之间导航。 1个活动中的3个片段使用MyPagerAdapter
这里是我的代码
片段
public class PageAdjuFragment extends VenteFragment {
Context context = getActivity();
public PageAdjuFragment() {
super();
// TODO Auto-generated constructor stub
}
public static PageAdjuFragment newInstance(String vente) {
PageAdjuFragment myFragment = new PageAdjuFragment();
Bundle args = new Bundle();
args.putString("vente", vente);
myFragment.setArguments(args);
return myFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
final View v = inflater.inflate(R.layout.page_adju_layout, container, false);
final Button b = (Button) v.findViewById(R.id.btconfp);
final RadioGroup radiopmt = (RadioGroup) v.findViewById(R.id.radiopmt);
final RadioButton rb1 = (RadioButton) v.findViewById(R.id.rb1);
final RadioButton rb2 = (RadioButton) v.findViewById(R.id.rb2);
final AutoCompleteTextView tv1 = (AutoCompleteTextView) v.findViewById(R.id.actv1);
final AutoCompleteTextView tv2 = (AutoCompleteTextView) v.findViewById(R.id.actv2);
final EditText tv3 = (EditText) v.findViewById(R.id.edt);
// Recovery of the name of th folder/vente
Bundle arg = getArguments();
// final String vente = arg.getString("vente");
VenteFragment.nomvente = arg.getString("vente");
try {
InitObjet();
InitPart();
} catch (IOException e) {
e.printStackTrace();
}
for (String elem : getListpart()) {
Log.v("part", elem);
}
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, this.getListobject());
tv2.setAdapter(adapter2);
//some useless code ////////////////
}
ActivityFragment
public class VenteActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager2);
creationPage();
}
public void creationPage() {
// Creation of the list
List<Fragment> fragments = new Vector<Fragment>();
// Recovery
Intent sender = getIntent();
String extraData = sender.getExtras().getString("vente");
Log.v("Vente activity", extraData);
// Add Fragments in a list
Fragment frag1 = PageOffreFragment.newInstance(extraData); // frag1 //
// anymor
fragments.add(frag1);
Fragment frag2 = PageAdjuFragment.newInstance(extraData); // frag2 //
// anymor
fragments.add(frag2);
Fragment frag3 = PagePartFragment.newInstance(extraData); // frag3 //
// anymor
fragments.add(frag3);
// Creation of theadapter
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
final ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager2);
// Affectation on the ViewPager
//pager.setOffscreenPageLimit(1);
pager.setAdapter(this.mPagerAdapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
}
});
}
// Overide for blocking the BACK Key of android device
@Override
public void onBackPressed() {
}
}
片段的工作类:
public class VenteFragment extends Fragment {
protected static List<String> Listobject = null;
protected static List<String> Listpart = null;
protected static List<String> Listclient = null;
protected static String nomvente;
public List<String> getListobject() {
return Listobject;
}
public void setListobject(List<String> listobject) {
Listobject = listobject;
}
public List<String> getListpart() {
return Listpart;
}
public void setListpart(List<String> listpart) {
Listpart = listpart;
}
public List<String> getListclient() {
return Listclient;
}
public void setListclient(List<String> listclient) {
Listclient = listclient;
}
// Create the List of participant
public void InitClient() throws IOException {...}
// Create the List of participant
public void InitPart() throws IOException {
Log.v("VFvente", this.nomvente);
File DIR = new File(Environment.getExternalStorageDirectory() + "/Vente Acta/ListeVente/" + this.nomvente);
final String ADDRESS_FILE = DIR.toString() + "/" + "participant.csv"; // l'emplacement
// de
// achat.csv
Log.v("VFchemin", ADDRESS_FILE);
// if the file doesn't exit, create the file
File f = new File(ADDRESS_FILE);
if (!f.exists()) {
f.createNewFile();
Log.v("VFvente", "ici");
return;
}
List<String> data = new ArrayList<String>(); // liste contenant les
// element a adapter
// aux
// autotextview
// on applique le reader sur le fichier
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
List<String[]> liste = reader.readAll();
int i = 0;
for (String[] ligne : liste) {
for (String col : ligne) {
Log.v("ligne " + i, "col " + col);
}
i++;
}
for (String[] elem : liste) { // pour chaque ligne
Log.v("VFdataelem0", elem[0]);
Log.v("VFdataelem1", elem[1]);
Log.v("VFdataelem2", elem[2]);
data.add(elem[1] + " " + elem[2]); // le nom et prenom
data.add(elem[0]); // le numero de participant pour la vente
}
reader.close();
Log.v("VFvente", "fin");
this.setListpart(data);
}
// Create the List of object
public void InitObjet() throws IOException {...}
}
页面适配器 公共类MyPagerAdapter扩展了FragmentStatePagerAdapter {
List<Fragment> fragments;
FragmentManager mFragmentManager;
// On fournit à l'adapter la liste des fragments à afficher
public MyPagerAdapter(FragmentManager fm, List fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int pos) {
return this.fragments.get(pos);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
我已经尝试过:
public int getItemPosition(Object object) {
return POSITION_NONE;
}
或adapter2.setNotifyOnChange(true);
或onResume
但可能在错误的地方
和
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int i) {
Log.d("onPageSelected: ", i + "");
if(i == 0) {
PageOffreFragment frg = (PageOffreFragment)mPagerAdapter.instantiateItem(pager, i);
frg.onResume();
} else if (i == 1){
PageAdjuFragment frg = (PageAdjuFragment)mPagerAdapter.instantiateItem(pager, i);
frg.onResume();
}else if (i == 2) {
PagePartFragment frg = (PagePartFragment)mPagerAdapter.instantiateItem(pager, i);
frg.onResume();
}
}
在VenteActiivty
没有任何作用:(
创建没问题,问题是我片段的生命周期。 (我认为) 并重新加载我的片段以更新我的ACTV数据列表
答案 0 :(得分:0)
在调用notifychange之前,请确保先将新数据添加到arraylist然后调用notify change
答案 1 :(得分:0)
最终我找到了一个解决方案:这是我的新FragmentActivity:
public class VenteActivity extends FragmentActivity {
private MyPagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.viewpager2);
creationPage();
}
public void creationPage() {
// Creation of the list
final List<Fragment> fragments = new Vector<Fragment>();
// Recovery
Intent sender = getIntent();
final String extraData = sender.getExtras().getString("vente");
Log.v("Vente activity", extraData);
// Add Fragments in a list
final Fragment frag1 = PageOffreFragment.newInstance(extraData); // frag1
fragments.add(frag1);
final Fragment frag2 = PageAdjuFragment.newInstance(extraData); // frag2
fragments.add(frag2);
final Fragment frag3 = PagePartFragment.newInstance(extraData); // frag3
fragments.add(frag3);
// Creation of theadapter
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
final ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager2);
// Affectation on the ViewPager
// pager.setOffscreenPageLimit(1);
pager.setAdapter(this.mPagerAdapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int i) {
Log.d("onPageSelected: ", i + "");
mPagerAdapter.notifyDataSetChanged();
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}