BaseAdapter On Scroll刷新ListView中的数据

时间:2017-11-13 09:14:18

标签: android listview android-fragments baseadapter

我是Java和Android的新手...我尝试使用BaseAdapter List创建一个ListView成功创建我有一个EditText以及每个列表项的按钮但真正的问题是我放了一些数据进入editText字段并向下滚动以更改最后一个列表项的值然后我回到顶部它将数据刷新为默认值它不包含用户在向下滚动This 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

我的BaseAdaper代码

    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;
    }
}

这是我如何将数据传递到我的适配器

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);

需要帮助: - 我希望在用户进入向上或向下滚动时保留相同的可见和编辑文本值...我希望我能够清楚地解释我的问题

1 个答案:

答案 0 :(得分:0)

单击按钮后,将其状态保存在布尔数组或其他位置。在getView方法中,检查以前是否单击了此按钮,然后相应地设置视图。

如果为行创建模型类会更好。