I have an application with a Listview that I insert a new row by a buttom, but when I rotate the phone, all inside the ListView disappear, and I have tried to retain the data and recreate the ListView, but whitout success... The array userArray with the correct data, but when I call the userAdapter.notifyDataSetChanged(); the ListView is not filled with the data...
public class Page1 extends Fragment
{
ListView userList;
private ItemsListAdapter userAdapter=null;
ArrayList<User> userArray = new ArrayList<User>();
@Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState");
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("key_userArray", userArray);
outState.putParcelable("key_ListView", userList.onSaveInstanceState());
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
// Restore last state.
userArray = savedInstanceState.getParcelableArrayList("key_userArray");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.page1, container, false);
Log.d(TAG, "onCreateView");
appContext = getActivity().getBaseContext();
appContextDialog = getActivity();
GappContext = appContext.getApplicationContext();
dBRicette = new ItemsSave();
thisFrag = this;
userList = null;
userListIng = null;
//userAdapter = null;
userAdapterIng = null;
myPref.loadPreference(appContext,MainActivity.DATABASE_INGR, MainActivity.ACCOUNT_PREFS_NAME);
init(rootView, savedInstanceState);
String myTag = getTag();
((MainActivity)getActivity()).setTabFragPage1(myTag);
int val = InitListaIngredienti();
if(val == 0){
}else
Toast.makeText(getActivity().getBaseContext(), getResources().getString(R.string.msg_problem_db), Toast.LENGTH_LONG).show();
return rootView;
}
void init(View rootView, Bundle savedInstanceState){
if(userAdapter == null ){
if(savedInstanceState == null || !savedInstanceState.containsKey("key_userArray")) {
userArray = new ArrayList<User>();
//userAdapter = new UserCustomAdapter(getActivity().getBaseContext(), R.layout.row, userArray);
userAdapter = new ItemsListAdapter(getActivity().getBaseContext(), userArray);
userList = (ListView) rootView.findViewById(R.id.listView);
userList.setItemsCanFocus(false);
userList.setClickable(true);
userList.setLongClickable(true);
userList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long id) {
// TODO Auto-generated method stub
RelativeLayout listItem = (RelativeLayout) v;
TextView clickedItemView = (TextView) listItem.findViewById(R.id.tv_prodotto);
String clickedItemString = clickedItemView.getText().toString();
Log.v(TAG,"DisplayListCustom LongClick detected " + clickedItemString + ", position " + Integer.toString(position));
if (currentActionMode != null) {
return false;
}
currentListItemIndex = position;//(User)v.getTag();
currentActionMode = getActivity().startActionMode(modeCallBack);
v.setSelected(true);
return true;
}
});
//userList.setOnItemSelectedListener(this);
userAdapter.notifyDataSetChanged();
userList.setAdapter(userAdapter);
}else {
//recupero i valori
mListInstanceState = savedInstanceState.getParcelable("key_ListView");
userArray = savedInstanceState.getParcelableArrayList("key_userArray");
userAdapter = new ItemsListAdapter(getActivity().getBaseContext(), userArray);
userList = (ListView) rootView.findViewById(R.id.listView);
userList.onRestoreInstanceState(mListInstanceState);
userList.setAdapter(userAdapter);
// int size = userArray.size();
// for(int i = 0; i < size; i++){
// userAdapter.UpdateIng(userArray.get(i).qta, userArray.get(i).name, TotVar);
// }
userAdapter.notifyDataSetChanged();
}
}else
userAdapter.notifyDataSetChanged();
class ItemsListAdapter extends BaseAdapter {
private LayoutInflater vi;
private ArrayList<User> data = new ArrayList<User>();
Context context;
User tmp = new User();
NumberFormat numberFormat0 = new DecimalFormat("#0.0");
public ItemsListAdapter(Context context, /*int layoutResourceId,*/ ArrayList<User> data) {
super();
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.data = data;
this.context = context;
}
/** Add white line */
public void addItem(User item) {
data.add(item);
notifyDataSetChanged();
}
public ArrayList<User> getData() {
return data;
}
public void setData(ArrayList<User> data) {
this.data = data;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = vi.inflate(R.layout.row3, parent, false);//null);
holder.grid = (GridLayout) convertView.findViewById(R.id.GridLayout);
holder.row = (TextView) convertView.findViewById(R.id.tv_row);
holder.name = (TextView) convertView.findViewById(R.id.tv_prodotto);
holder.quantity = (TextView) convertView.findViewById(R.id.tv_qta);
holder.textPercT = (TextView) convertView.findViewById(R.id.tv_perc_T);
holder.textPercZ = (TextView) convertView.findViewById(R.id.tv_perc_Z);
holder.textPercG = (TextView) convertView.findViewById(R.id.tv_perc_G);
holder.textUm = (TextView) convertView.findViewById(R.id.tv_grm);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}