片段列表视图中带有edittext和复选框的随机行为

时间:2018-09-20 15:43:40

标签: android listview android-fragments

在选中复选框并将文本插入到listvew的edittext中时,无论滚动还是不滚动,它们似乎都在列表项之间有些随机迁移。
我试图实现持有人和其他解决方案,这些解决方案可以在以下链接中找到,但无济于事。

CheckBox in RecyclerView keeps on checking different items
Checking a checkbox in listview makes other random checkboxes checked too

任何与我有关困境的建议都将不胜感激。

这是上述程序的代码。

ItemAdapter

public class ItemAdapter extends ArrayAdapter<Item> {

public static boolean isitpopulated;
public static int DrawerInUse;
public static int ListLoc;


public ItemAdapter(Activity context, ArrayList<Item> items) {
    super(context, 0, items);
}

@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
    // Check if the existing view is being reused, otherwise inflate the view
    View listItemView = convertView;

    if (listItemView == null) {
        listItemView = LayoutInflater.from(getContext()).inflate(
                R.layout.list_item, parent, false);

    }
        // Get the {@link Item} object located at this position in the list
        final Item currentItem = getItem(position);
        final Item currentposs = getItem(position);

        // Find the TextView in the list_item.xml layout with the ID version_name
        TextView itemTextView = (TextView) listItemView.findViewById(R.id.item);

        CheckBox check = listItemView.findViewById(R.id.checkbox);

        final TextView quantityTextView = (TextView) listItemView.findViewById(R.id.quantity);
        String listcontent = quantityTextView.getText().toString();

        int drawer = Drawers.DrawerChosen;            

        // Get the version name from the current object and
        // set this text on the name TextView
        itemTextView.setText(currentItem.getItem());

        TextView infoTextView = (TextView) listItemView.findViewById(R.id.info);
        infoTextView.setText(currentItem.getInfo());

        TextView doseTextView = (TextView) listItemView.findViewById(R.id.dose);
        doseTextView.setText(currentItem.getDose());

        TextView typeTextView = (TextView) listItemView.findViewById(R.id.type);
        typeTextView.setText(currentItem.getType());
        // website with solution
        //https://stackoverflow.com/questions/11190390/checking-a-checkbox-in-listview-makes-other-random-checkboxes-checked-too

    if (listcontent.isEmpty()) {
            quantityTextView.setText(currentItem.getQuantity());

            System.out.println("Drawer: "+drawer+" Position: "+position);

            if (drawer < 9) {
                String Liststatus = ListProcessing.wagon[0][drawer][position][5];
                if (Liststatus.equals("true")) {
                    // make box cheked
                    CheckBox checktemp = listItemView.findViewById(R.id.checkbox);
                    checktemp.setChecked(true);
                    System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA: " + ListProcessing.wagon[0][drawer][position][5]);
                }
            }

            if (drawer == 10) {
            //String FListstatus = ListProcessing.finalWagon[position][5];
                /*
                if (FListstatus.equals("true")) {
                    CheckBox checktemp = listItemView.findViewById(R.id.checkbox);
                    checktemp.setChecked(true);
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
                }
                */
            }

            // if(listcontent.isEmpty()) {
            //final CheckBox check = listItemView.findViewById(R.id.checkbox);

            final View listItemView1 = listItemView;

            //reset and Populating list
            final String itemName = currentposs.getItem();
            String itemInfo = currentposs.getInfo();
            String itemDose = currentposs.getDose();
            String itemQuantity = currentposs.getQuantity();
            String itemType = currentposs.getType();
            // populateList(position,itemName,itemInfo,itemDose,itemQuantity,itemType);

            // when chekbox is ticked then list is amended
            check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox checkBox = (CheckBox) v;
                    ListProcessing d = new ListProcessing();
                    if (checkBox.isChecked()) {

                        if (Drawers.DrawerChosen <= 10) {
                            d.wagon[0][Drawers.DrawerChosen][position][5] = "true";                                
                        } else if (Drawers.DrawerChosen == 10) {
                            d.finalWagon[position][5] = "true";
                        }
                    } else if (checkBox.isChecked() == false) {
                        if (Drawers.DrawerChosen < 10) {
                            d.wagon[0][Drawers.DrawerChosen][position][5] = "false";
                        } else if (Drawers.DrawerChosen == 10) {
                            d.finalWagon[position][5] = "false";
                        }
                    }
                }
            });

            final TextView quantityTextView1 = (TextView) listItemView1.findViewById(R.id.quantity);
            String userinput = quantityTextView1.getText().toString();
           listItemView.findViewById(R.id.quantity);
            quantityTextView1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    // System.out.println("!!!!!!!!!!!!!!!!!!!!!!beforeTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    //System.out.println("!!!!!!!!!!!!!!!!!!!!!!onTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                }

                //@Override
                public void afterTextChanged(Editable s) {
                    System.out.println("!!!!!!!!!!!!!!!!!!!!!!afterTextChanged!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                    String numberInserted;
                    final View listItemView2 = listItemView1;
                    final TextView quantityTextView2 = (TextView) listItemView2.findViewById(R.id.quantity);
                    String userinput = quantityTextView2.getText().toString();
                    //System.out.println("user input: " + userinput + "-------------------------------");
                    //if (userinput.isEmpty()) {

                    int drawer = Drawers.DrawerChosen;
                    //ListProcessing.wagon[0][drawer][position][3] = userinput;
                    //System.out.println("ListProcessing.wagon[0]["+drawer+"]["+position+"][3]: " + ListProcessing.wagon[0][drawer][position][3] + "-------------------------------");
                    ListProcessing.ChangeData(drawer, position, userinput);
                }
            });
        }


        if (currentItem.isCheckBox()) {
            check.setVisibility(View.VISIBLE);
        } else {
            check.setVisibility(View.GONE);
        }

        if (currentItem.isInfo()) {
            infoTextView.setVisibility(View.VISIBLE);
        } else {
            infoTextView.setVisibility(View.GONE);
        }

        if (currentItem.isDose()) {
            doseTextView.setVisibility(View.VISIBLE);
        } else {
            doseTextView.setVisibility(View.GONE);
        }

        if (currentItem.isQuantity()) {
            quantityTextView.setVisibility(View.VISIBLE);
        } else {
            quantityTextView.setVisibility(View.GONE);
        }


    return listItemView;
}
}


package com.example.notandi.hospitalwagons;


项目

public class Item {

private String item;
private String info;
private String dose;
private String type;
private int quantity;
private int userQuantity;

private boolean isInfo = false;
private boolean isDose = false;
private boolean isQuantity = false;
private boolean isCheckBox = false;
public int itemChosen;

/**
 * @param wItem
 */

public Item(String wItem) {
    item = wItem;
    isCheckBox = true;
}


/**
 * @param wItem is the name of the item/medicine
 * @param wQuantity is how many items there should be total
 * @param wType ...
 */
public Item(String wItem, int wQuantity, String wType) {
    item = wItem;
    quantity = wQuantity;
    type = wType;
    isCheckBox = true;
    isQuantity = true;
}

public Item(String wItem, String wInfo, String wDose, int wQuantity, String wType) {
    item = wItem;
    info = wInfo;
    dose = wDose;
    quantity = wQuantity;
    type = wType;
    isCheckBox = true;
    isQuantity = true;
    isDose = true;
    isInfo = true;
}

public String getItem() {
    return item;
}

public String getInfo() {
    return info;
}

public String getDose() {
    return dose;
}

public String getType() {
    return type;
}

public String getQuantity() {
    return Integer.toString(quantity);
}

public boolean isCheckBox() {
    return isCheckBox;
}

public boolean isInfo() {
    return isInfo;
}

public boolean isDose() {
    return isDose;
}

public boolean isQuantity() {
    return isQuantity;
}

public void findViewById (int quantity) {
}
}

许多碎片之一

public class drawer_med_Fragment extends Fragment {

public int[] quantityArr;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    getActivity().setTitle(R.string.medicine_drawer);

    ArrayList<Item> items = new ArrayList<Item>();

    String[] itemArr = getResources().getStringArray(R.array.medicine_drawer_items);
    String[] infoArr = getResources().getStringArray(R.array.medicine_drawer_info);
    String[] doseArr = getResources().getStringArray(R.array.medicine_drawer_dose);
    String[] typeArr = getResources().getStringArray(R.array.medicine_drawer_type);
    int[] quantityArr= new int[26];
    for(int i=0;i<26;i++){
        int a = Integer.parseInt(ListProcessing.wagon[0][1][i][3]);
        quantityArr[i]=a;
        }

    String item;
    String info;
    String dose;
    String type;
    int quantity;

    for (int i = 0; i < itemArr.length; i++) {

        item = itemArr[i];
        info = infoArr[i];
        dose = doseArr[i];
        quantity = quantityArr[i];
        type = typeArr[i];
        items.add(new Item(item, info, dose, quantity, type));
    }

    ItemAdapter adapter = new ItemAdapter(getActivity(), items);

    ListView listView = (ListView) rootView.findViewById(R.id.list);

    listView.setAdapter(adapter);

    return rootView;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setRetainInstance(true);
}
//public void startlistTop(){
  //  quantityArr =  getResources().getIntArray(R.array.medicine_drawer_quantity);
//}
  }

1 个答案:

答案 0 :(得分:2)

适配器最有可能出现此问题。每个if语句都必须添加else语句。像这样的东西:

if (listcontent.isEmpty()) {
  quantityTextView.setText(currentItem.getQuantity());
} else {
  quantityTextView.setText("");
}

if (drawer < 9) {
...
} else {
...
}