Android ListView中滚动刷新数据时的BaseAdapter

Android ListView中滚动刷新数据时的BaseAdapter,android,listview,android-fragments,baseadapter,Android,Listview,Android Fragments,Baseadapter,我对Java和android非常陌生。。。我正在尝试使用BaseAdapter创建一个ListView列表创建成功每个列表项都有一个EditText和按钮,但真正的问题是当我将一些数据放入EditText字段并向下滚动以更改最后一个列表项的值时,我返回顶部,它将数据刷新为默认值,但不包含在向下滚动之前由用户输入 我的BaseAdaper代码 class CoustomAdptr extends BaseAdapter{ String[] dates; Integer[

我对Java和android非常陌生。。。我正在尝试使用BaseAdapter创建一个ListView列表创建成功每个列表项都有一个EditText和按钮,但真正的问题是当我将一些数据放入EditText字段并向下滚动以更改最后一个列表项的值时,我返回顶部,它将数据刷新为默认值,但不包含在向下滚动之前由用户输入

我的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;
    }
}
类CoustomAdptr扩展BaseAdapter{ 字符串[]日期; 整数[]存货; 整数房间; public CoustomAdptr(RoomFragment RoomFragment,字符串[]日期,整数[]库存,整数totalrooms){ this.dates=日期; 这个。存货=存货; this.totalrooms=totalrooms; } @凌驾 public int getCount(){ 返回日期。长度; } @凌驾 公共对象getItem(int i){ 返回null; } @凌驾 公共长getItemId(int i){ 返回0; } @凌驾 公共视图getView(最终int i、视图视图、视图组视图组){ 视图=GetLayoutFlater().充气(R.layout.inventory\u 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); 如果(库存[i]==0){ setBackgroundColor(getResources().getColor(R.color.SoldOut)); editText.setTextColor(getResources().getColor(R.color.SoldOutTextColor)); }else if(库存[i] 这就是我向适配器传递数据的方式

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);
JSONObject jObj=parentObject.getJSONObject(“成功”);
JSONObject jObj2=jObj.getJSONObject(“数据”);
JSONArray arrajson=jObj2.getJSONArray(“库存”);
字符串arrayCount=Integer.toString(arrajson.length());
String[]dates=新字符串[arrajson.length()];
整数[]库存=新整数[arrajson.length()];
整数TotalRoom=新整数(jObj2.getInt(“total_room”);

对于(int i=0;i,单击按钮后,将其状态保存在布尔数组或其他地方。在getView方法中,检查此按钮之前是否单击过,然后相应地设置视图


最好是为行创建一个模型类。

将列表视图中的每个项的选定状态存储为
布尔值
,同时从上到下或从下到上滚动,它将搜索
getView()
方法,然后检查
isSelected
是否为真,并根据它更改状态