Search code examples
androidlistviewandroid-fragmentsbaseadapter

BaseAdapter On Scroll Refresh Data in ListView


I'm Very new to Java and android... I'm Try to create an ListView using BaseAdapter List being created successfully i have a EditText along with button for each list item but the real problem is when i put some data into editText Field and scroll down to change value of last list item then i go back to the top it refreshes the data to default value it doesn't contain the value which was entered by user before scrolling downThis Image is Before Scrolling This Image is after scroll - Check first image there is visual changes in update button but once i scroll down and go back to top again it lose all visual change is button

My BaseAdaper Code

    class CoustomAdptr extends BaseAdapter{

    String[] dates;
    Integer[] inventory;
    Integer totalrooms;
    public CoustomAdptr(RoomFragment roomFragment, String[] dates, Integer[] inventory, Integer totalrooms) {
        this.dates = dates;
        this.inventory = inventory;
        this.totalrooms = totalrooms;
    }

    @Override
    public int getCount() {
        return dates.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        view = getLayoutInflater().inflate(R.layout.inventory_listview,null);
        TextView textView = (TextView) view.findViewById(R.id.roomListViewText);
        final EditText editText = (EditText) view.findViewById(R.id.roomListInventory);
        final Button updateButton = (Button) view.findViewById(R.id.roomListViewInventoryUpdateButton);
        if(inventory[i] == 0){
            editText.setBackgroundColor(getResources().getColor(R.color.SoldOut));
            editText.setTextColor(getResources().getColor(R.color.SoldOutTextColor));
        } else if(inventory[i] < totalrooms){
            editText.setBackgroundColor(getResources().getColor(R.color.invetory));
            editText.setTextColor(getResources().getColor(R.color.invetoryTextColor));
        } else if(inventory[i] == totalrooms){
            editText.setBackgroundColor(getResources().getColor(R.color.fullInventory));
            editText.setTextColor(getResources().getColor(R.color.fullInventoryTextColor));
        }

        editText.setText(String.valueOf(inventory[i]));
        textView.setText(dates[i]);
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //String name = editText.getText().toString();
                //String name1 = dates[i];
                //String name2 = getArguments().getString("room_id");
                updateButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_done_black_24dp,0,0,0);
                //updateButton.setBackgroundColor(getResources().getColor(R.color.SoldOut));
                updateButton.setText("Updated");
                updateButton.setEnabled(false);
                Toast.makeText(getContext(), "Update Inventory Button Clicked", Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }
}

This is How Im Passing Data to My Adapter

JSONObject jObj = parentObject.getJSONObject("success");
JSONObject jObj2 = jObj.getJSONObject("data");
JSONArray arrajson = jObj2.getJSONArray("inventories");
String arrayCount = Integer.toString(arrajson.length());
String[] dates = new String[arrajson.length()];
Integer[] inventory = new Integer[arrajson.length()];
Integer totalrooms = new Integer(jObj2.getInt("total_room"));
for (int i=0; i<arrajson.length();i++){
     JSONObject jsonObject = arrajson.getJSONObject(i);
     dates[i] = jsonObject.getString("date");
     inventory[i] = jsonObject.getInt("inventory");
}
CoustomAdptr coustomAdptr = new CoustomAdptr(RoomFragment.this,dates,inventory,totalrooms);
listView.setAdapter(coustomAdptr);

Help Needed :- I Want to retain same visible and Value of edittext as users enters on scroll up or down... i hope i was able to explain my problem clearly


Solution

  • After clicking a button, save it's state in a boolean array or somewhere else. And inside getView method, check if this button was previously clicked or not then setup your view accordingly.

    It would be better if you create a model class for rows.