Kashyap Asrc Addepalli Kashyap Asrc Addepalli - 9 days ago 9
Android Question

updation of UI with wrong values in listview

Please excuse my English. I have an activity consists of two fragments with viewpager. And every fragment has a listview.

I am using this data in many locations, so i have created this as static.

class DataHandler {

static ArrayList<MenuHolder> listOfItemsFromJson = new ArrayList<MenuHolder>();


public static ArrayList<MenuHolder> getListOfItemsFromJson() {
return listOfItemsFromJson;
}


public static void addData(MenuHolder holder) {
listOfItemsFromJson.add(holder);

}

public static MenuHolder getData(int position) {

return listOfItemsFromJson.get(position);

}


public static void modifyData(MenuHolder holder, int position) {

listOfItemsFromJson.set(position, holder);
}
public static int size() {

return listOfItemsFromJson.size();
}
}


Here, My fragment code. In onCreateView(), I am clearing the previous data &
adding the fresh data.

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

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

mlistView = (ListView) view.findViewById(R.id.mListView);

//here I am clearing the previous data and loading new data

for (int i = 0; i < DataHandler.size(); i++) {

if (DataHandler.getData(i).isCombo()) {
DataHandler.removeData(i);
}

}


for (int i = 0; i < 3; i++) {
MenuHolder mMenuHolder = new MenuHolder();

mMenuHolder.setItemname(itemNames[i]);
mMenuHolder.setImageName("@mipmap/ic_launcher");
mMenuHolder.setItemCount("0");
mMenuHolder.setItemprice("500");
mMenuHolder.setUserSeleted(false);
mMenuHolder.setCombo(true);

DataHandler.addData(mMenuHolder);

}
index.clear();
for (int i = 0; i < DataHandler.size(); i++) {
if (DataHandler.getListOfItemsFromJson().get(i).isCombo()) {
index.add(i);
}
}

mCombosAdapter = new ComboMenuAdapter(getActivity(), index);

mlistView.setVisibility(View.VISIBLE);
mlistView.setAdapter(mCombosAdapter);


return view;
}


Here I am getting the wrong values. DataHandler.size() is wrong.

for (int i = 0; i < DataHandler.size(); i++) {
if (DataHandler.getListOfItemsFromJson().get(i).isCombo()) {
index.add(i);
}
}


Here, I am navigating to another activity and having some modifications in dataHandler class & after coming back the list size is wrong & list size is increasing.

Answer

I think your DataHandler should as a singleton;

class DataHandler {
    private static DataHandler mInstance = null;
    ArrayList<MenuHolder> listOfItemsFromJson;

    public DataHandler getInstance() {
        if(null == mInstance) {
           mInstance = new DataHandler();
       }
    }
    public DataHandler() {
          listOfItemsFromJson = new ArrayList<MenuHolder>();
    }

    public ArrayList<MenuHolder> getListOfItemsFromJson() {
        return listOfItemsFromJson;
    }


    public void addData(MenuHolder holder) {
        listOfItemsFromJson.add(holder);

    }

    public MenuHolder getData(int position) {

        return listOfItemsFromJson.get(position);

    }


    public void modifyData(MenuHolder holder, int position) {

             listOfItemsFromJson.set(position, holder);
     }
    public int size() {

        return listOfItemsFromJson.size();
    }
}

You can user it like : DataHandler.getInstance().size();

But i think you set dataHandler as static is ugly... you can create a adapter replace DataHandler, then your all listview can use this adapter.