当我的Android应用更改片段或将设备位置更改为横向时,我遇到了问题。列表视图中的内容将被删除/删除。我已经阅读了一些有关它的信息,我将需要在“onSaveInstanceState”下保存listview的内容/数据,并在重新创建片段时使用“onCreate”将这些保存的内容/数据放回listview中。但我不确定在“onSaveInstnceState”部分下要键入什么。请帮我这个。据我所知,有人说这很容易克服,但对我来说,这似乎是一个需要克服的巨大障碍。
以下是代码。有些部分已经改变了。
public class Tab extends Fragment {
ArrayList<ExpaneseModel> beta1;
ListView listView;
private static ExpansesCustomAdapter adapter;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
}else{
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.tab2, container, false);
Button Add = (Button)rootview.findViewById(R.id.btnAdd);
listView=(ListView)rootview.findViewById(R.id.list);
beta1= new ArrayList<>();
listView.setAdapter(adapter);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialogsetting = new Dialog(getActivity());
dialogsetting.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogsetting.setContentView(R.layout.entry_input);
final EditText one = (EditText)dialogsetting.findViewById(R.id.et_one);
final EditText two = (EditText)dialogsetting.findViewById(R.id.et_two);
Button ok = (Button)dialogsetting.findViewById(R.id.btnOK);
Button cancel = (Button)dialogsetting.findViewById(R.id.btnCancel);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataModels.add(new ExpaneseModel(one.getText().toString(), two.getText().toString()));
adapter.notifyDataSetChanged();
dialogsetting.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogsetting.dismiss();
}
});
dialogsetting.show();
}
});
return rootview;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//I am not sure what code to type in here
} }
以下是ExpaneseModel:
公共类ExpaneseModel {
String one;
String two;
public ExpaneseModel(String one, String two) {
this.one=one;
this.two=two;
}
public String getOne() {
return one;
}
public String getTwo() {
return two;
}}
以下是自定义适配器类:
public class ExpansesCustomAdapter extends ArrayAdapter<ExpaneseModel> implements View.OnClickListener{
private ArrayList<ExpaneseModel> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView txtOne;
TextView txtTwo;
}
public ExpansesCustomAdapter(ArrayList<ExpaneseModel> data, Context context) {
super(context, R.layout.expanses_row_item, data);
this.dataSet = data;
this.mContext=context;
}
@Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
ExpaneseModel dataModel=(ExpaneseModel) object;
}
private int lastPosition = -1;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
ExpaneseModel dataModel = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.expanses_row_item, parent, false);
viewHolder.txtOne = (TextView) convertView.findViewById(R.id.one);
viewHolder.txtTwo = (TextView) convertView.findViewById(R.id.two)
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);
lastPosition = position;
viewHolder.txtName.setText(dataModel.getName());
viewHolder.txtType.setText(dataModel.getAmount());
viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
答案 0 :(得分:0)
问题是,当您回到Fragment
时,onCreateView()
会致电。所以一切都会出错。我通过以下步骤解决了这个问题:
rootView
变量保存为类变量。所以你保存了rootView
。onCreateView()
之前rootview = inflater.inflate(R.layout.tab2, container, false);
之前,检查rootView
是否为空。如果不是,则返回rootView
。这种方式片段不会被重新创建而且没有任何变化。你的最终代码应该是这样的:
public class Tab extends Fragment {
ArrayList<ExpaneseModel> beta1;
ListView listView;
private static ExpansesCustomAdapter adapter;
private View rootView;
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
}else{
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView != null)
return rootView;
rootview = inflater.inflate(R.layout.tab2, container, false);
Button Add = (Button)rootview.findViewById(R.id.btnAdd);
listView=(ListView)rootview.findViewById(R.id.list);
beta1= new ArrayList<>();
listView.setAdapter(adapter);
Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialogsetting = new Dialog(getActivity());
dialogsetting.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogsetting.setContentView(R.layout.entry_input);
final EditText one = (EditText)dialogsetting.findViewById(R.id.et_one);
final EditText two = (EditText)dialogsetting.findViewById(R.id.et_two);
Button ok = (Button)dialogsetting.findViewById(R.id.btnOK);
Button cancel = (Button)dialogsetting.findViewById(R.id.btnCancel);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataModels.add(new ExpaneseModel(one.getText().toString(), two.getText().toString()));
adapter.notifyDataSetChanged();
dialogsetting.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogsetting.dismiss();
}
});
dialogsetting.show();
}
});
return rootview;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//I am not sure what code to type in here
}
}