gpt4 book ai didi

java - Android Fragment 在第二次单击选项卡之前不会加载数据

转载 作者:行者123 更新时间:2023-12-02 10:09:06 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序来跟踪咖啡消耗量,我有 5 个选项卡,如附图所示。在其中一个选项卡( fragment )中,我有一个 viewPager,用于在饼图中显示数据。我遇到一个奇怪的问题,即仅当我第二次单击 fragment 时才显示数据。最初,它看起来像这样 Pie chart on the first click 。当我第二次点击时,它会添加信息。 Piechart on the second click 。用于显示数据的包是https://github.com/PhilJay/MPAndroidChart .

我不确定到底哪里出了问题,但我已经附加了 TrackingFragment 和一些 CostFragment( fragment 内的 fragment )的代码。数据是从解析服务器客户端获取的,但我相当确定问题出在 fragment 的初始化上。

感谢任何帮助。

跟踪 fragment

public class TrackingFragment extends Fragment {

private static ArrayList<String> drinkNames;
private static String TAG = "TrackingFragment";
CaffeineFragment caffeineFragment;
CostFragment costFragment;
QuantityFragment quantityFragment;

public TrackingFragment() {
// Required empty public constructor
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

costFragment = new CostFragment();
caffeineFragment = new CaffeineFragment();
quantityFragment = new QuantityFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tracking, container, false);

ViewPager pager = view.findViewById(R.id.viewPager);

initViewPager(pager);

TabLayout tabLayout = view.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(pager);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);

return view;
}

private void initViewPager(ViewPager pager) {

FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager());

adapter.addFragment(costFragment, "Price");
adapter.addFragment(caffeineFragment, "Caffeine");
adapter.addFragment(quantityFragment, "Quantity");
pager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}


static class FragmentAdapter extends FragmentPagerAdapter{

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public FragmentAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}

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


public void addFragment(Fragment frag, String title){
fragmentList.add(frag);
fragmentTitleList.add(title);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
}

}

成本 fragment

public class CostFragment extends Fragment {

private static final String TAG = "CostFragment";
public static ArrayList<Double> drinksPrice = new ArrayList<>();
public static ArrayList<String> drinksName = new ArrayList<>();
private PieChart piechart;
public static ArrayList<String> dates = new ArrayList<>();
private BarChart barchart;
private ArrayList<PieEntry> pieEntries = new ArrayList<>();
private ArrayList<BarEntry> barEntries = new ArrayList<>();


@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.tracking_layout_fragment, container, false);

return view;
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Log.i(TAG, "onCreate: " + drinksPrice.toString());

getPieData();
getBarData();
}

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

piechart = view.findViewById(R.id.pieChart);
barchart = view.findViewById(R.id.barChart);
piechart.setRotationEnabled(true);

getParseInfo();

addBarData();
addPieData();

}

getParseInfo() 和 addPieData()

  private void getParseInfo() {

drinksName.clear();
drinksSize.clear();
dates.clear();

ParseQuery parseQuery = ParseQuery.getQuery(Drink.class);
parseQuery.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (ParseObject object : objects) {
drinksSize.add(object.getInt("size"));
drinksName.add(object.getString("itemName"));
dates.add(getDate(object.getCreatedAt()));
}
}else{
Log.i(TAG, "exception: " + e.getMessage());
}
}
});
}


private void addPieData() {

PieDataSet pieDataSet = new PieDataSet(pieEntries, "Each item volume (ml)");
pieDataSet.setSliceSpace(1);
pieDataSet.setValueTextSize(14);
pieDataSet.setColors(getColours());
PieData data = new PieData((pieDataSet));
piechart.setData(data);
piechart.animateY(1000);
piechart.setExtraRightOffset(10f);
Legend legend = piechart.getLegend();
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.CENTER);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
legend.setOrientation(Legend.LegendOrientation.VERTICAL);
piechart.getDescription().setEnabled(false);
piechart.setDrawEntryLabels(false);
piechart.invalidate();

}

private void getPieData(){

for(int i = 0;i<drinksName.size();i++){
pieEntries.add(new PieEntry(drinksSize.get(i).floatValue(),drinksName.get(i)));
}

}

最佳答案

将以下行从 onViewCreated() 移至 onCreateView():

piechart = view.findViewById(R.id.pieChart);
barchart = view.findViewById(R.id.barChart);
piechart.setRotationEnabled(true);

getParseInfo();

之后在 for 循环后的 getParseInfo() 中添加以下行:

parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null)
{
for (ParseObject object : objects) {
drinksSize.add(object.getInt("size"));
drinksName.add(object.getString("itemName"));
dates.add(getDate(object.getCreatedAt()));
}
getPieData();
getBarData();
}else{
Log.i(TAG, "exception: " + e.getMessage());
}
}
});

发生这种情况是因为当您调用 getParseInfo() 时,它将转到后台,并且在获得结果之前调用 getPieData() 。因此,为了避免这种情况,请应用建议的更改。

关于java - Android Fragment 在第二次单击选项卡之前不会加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55121761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com