gpt4 book ai didi

java - ListFragments 不会自动恢复数据集吗?

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:29 27 4
gpt4 key购买 nike

我有一个扩展 ListFragment 的 fragment ,当我最小化应用程序并返回到它时, ListView 为空。

不会自动恢复吗?

public class LocationListViewFrag extends ListFragment implements AdapterView.OnItemClickListener {

private static final String TAG = "LocationListViewFrag";

private ArrayList<LocationObj> mLocations = new ArrayList<>();
private LocationAdapter mTaxAdapter;
private LocationListViewListener mListener;

@Bind(R.id.listLabel)
TextView listLabel;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTaxAdapter = new LocationAdapter(getActivity(), mLocations);
setListAdapter(mTaxAdapter);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_tax_location_list, container, false);
ButterKnife.bind(this, v);
return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(this);

}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (LocationListViewListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement LocationListViewListener");
}
}

public void setLocations(ArrayList<LocationObj> Locations) {
mLocations = Locations;

mTaxAdapter.clear();
mTaxAdapter.addAll(mLocations);
mTaxAdapter.notifyDataSetChanged();

if(mLocations.isEmpty()){
listLabel.setVisibility(View.VISIBLE);
} else {
listLabel.setVisibility(View.GONE);
}
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

LocationObj aLocation = mLocations.get(position);
Log.d(TAG, String.format("Clicked - %s", aLocation.toString()));

mListener.onLocationSelected(aLocation);
}

public interface LocationListViewListener {

void onLocationSelected(LocationObj LocationObj);

}
}

Activity

onCreate

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

setContentView(R.layout.free_tax_prep_providers_new);
initDrawer();
ButterKnife.bind(this);

if (savedInstanceState != null) {
//Restore the fragment's instance
mLocationListViewFrag = (LocationListViewFrag)getSupportFragmentManager().getFragment(savedInstanceState, "mLocationListViewFrag");

} else if (findViewById(R.id.fragment_container) != null) {
// Check that the activity is using the layout version with
// the fragment_container FrameLayout

// Create a new Fragment to be placed in the activity layout
mLocationListViewFrag = new LocationListViewFrag();

// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, mLocationListViewFrag).commit();

}

}

onSavedInstance

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

//Save the fragment's instance
getSupportFragmentManager().putFragment(outState, "mLocationListViewFrag", mLocationListViewFrag);

}

最佳答案

您实际上可以在 fragment 本身而不是 Activity 中重写 onSaveInstanceState。当我处理列表 fragment 时,这样做就完成了我的工作。

关于java - ListFragments 不会自动恢复数据集吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35879211/

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