listView中的EditText无法保存输入,android?

时间:2016-06-27 06:03:11

标签: android listview android-edittext

我在list元素中有一个包含3个编辑文本的列表视图。编辑文本输入字符串的状态不会保持固定在元素的位置。

我创建了一个存储值的数组,但它复制了所有编辑文本中的值。

这是我的适配器类

public class OrderAdapter extends BaseAdapter {
private ArrayList<POJOOrder> assetArrayList;
private String[] agreedVolume, agreedSalesParty, agreedSalesMgr;

public OrderAdapter(ArrayList<POJOOrder> assetArrayList) {
    this.assetArrayList = assetArrayList;
    agreedVolume = new String[assetArrayList.size()];
    agreedSalesParty = new String[assetArrayList.size()];
    agreedSalesMgr = new String[assetArrayList.size()];
}

@Override
public int getCount() {
    return assetArrayList.size();
}

@Override
public Object getItem(int position) {
    return assetArrayList.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                parent.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        vi = mInflater.inflate(R.layout.element_order, null);
        holder = new ViewHolder();
        holder.tvItem = (TextView) vi.findViewById(R.id.textViewItem);
        holder.tvPlanned = (TextView) vi.findViewById(R.id.textViewPlanned);
        holder.tvPlannedParty = (TextView) vi.findViewById(R.id.textViewPalnnedParty);
        holder.tvPlannedMgr = (TextView) vi.findViewById(R.id.textViewPlannedMgr);
        holder.etAgreed = (EditText) vi.findViewById(R.id.editTextAgreed);
        holder.etAgreedParty = (EditText) vi.findViewById(R.id.editTextAgreedParty);
        holder.etAgreeMgr = (EditText) vi.findViewById(R.id.editTextAgreedMgr);
       //            holder.etAgreed.setTag(position);
      //            holder.etAgreedParty.setTag(position);
     //            holder.etAgreeMgr.setTag(position);
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    holder.number = position;
    holder.tvItem.setText(assetArrayList.get(position).getBrandWith_SKU());
    holder.tvPlanned.setText(assetArrayList.get(position).getPlannedVolume() + " CS");
    holder.tvPlannedParty.setText(assetArrayList.get(position).getPlannedSalesPromoParty() + "");
    holder.tvPlannedMgr.setText(assetArrayList.get(position).getPlannedSalesPromoMgr() + "");

    if (agreedVolume[holder.number] != null) {
        holder.etAgreed.setText("" + agreedVolume[position]);
    }

    if (agreedSalesParty[position] != null) {
        holder.etAgreedParty.setText("" + agreedSalesParty[position]);
    }

    if (agreedSalesMgr[position] != null) {
        holder.etAgreeMgr.setText("" + agreedSalesMgr[position]);
    }


    holder.etAgreed.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            Log.e("position", holder.number + "");

            agreedVolume[holder.number] = s.toString();
        }
    });
    holder.etAgreedParty.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            agreedSalesParty[holder.number] = s.toString();
        }
    });
    holder.etAgreeMgr.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            agreedSalesMgr[holder.number] = s.toString();
        }
    });

    return vi;
}

private static class ViewHolder {
    private TextView tvItem, tvPlanned, tvPlannedParty, tvPlannedMgr;
    private EditText etAgreed, etAgreedParty, etAgreeMgr;
    private int number;
}

}

POJOOrder只是一个带有getter和setter的pojo类

public class POJOOrder {
private int BrandId;
private String BrandWith_SKU;
private int SKU_Id;
private int PlannedVolume;
private int PlannedSalesPromoParty;
private int PlannedSalesPromoMgr;

public POJOOrder(int brandId, String brandWith_SKU, int SKU_Id, int plannedVolume, int plannedSalesPromoParty, int plannedSalesPromoMgr) {
    BrandId = brandId;
    BrandWith_SKU = brandWith_SKU;
    this.SKU_Id = SKU_Id;
    PlannedVolume = plannedVolume;
    PlannedSalesPromoParty = plannedSalesPromoParty;
    PlannedSalesPromoMgr = plannedSalesPromoMgr;
}

public int getBrandId() {
    return BrandId;
}

public String getBrandWith_SKU() {
    return BrandWith_SKU;
}

public int getSKU_Id() {
    return SKU_Id;
}

public int getPlannedVolume() {
    return PlannedVolume;
}

public int getPlannedSalesPromoParty() {
    return PlannedSalesPromoParty;
}

public int getPlannedSalesPromoMgr() {
    return PlannedSalesPromoMgr;
}

}

这是列表元素XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textViewItem"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="Item Code"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textViewPlanned"
        android:layout_width="90dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="0 CS "
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textViewPalnnedParty"
        android:layout_width="90dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="0"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/textViewPlannedMgr"
        android:layout_width="90dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:text="0"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/editTextAgreed"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:hint="000"
        android:inputType="number"
        android:maxLength="3"
        android:textColor="@android:color/black"
        android:textSize="15sp" />


    <EditText
        android:id="@+id/editTextAgreedParty"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:hint="000"
        android:inputType="number"
        android:maxLength="3"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <EditText
        android:id="@+id/editTextAgreedMgr"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:hint="000"
        android:inputType="number"
        android:maxLength="3"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

因此,如果您没有这些字段的存储值,我认为您忘记清除字段,基本上尝试代替此:

if (agreedVolume[holder.number] != null) {
    holder.etAgreed.setText("" + agreedVolume[position]);
}

if (agreedSalesParty[position] != null) {
    holder.etAgreedParty.setText("" + agreedSalesParty[position]);
}

if (agreedSalesMgr[position] != null) {
    holder.etAgreeMgr.setText("" + agreedSalesMgr[position]);
}

类似的东西:

holder.etAgreed.setText(agreedVolume[holder.number] != null ? "" + agreedVolume[position] : "");
holder.etAgreedParty.setText(agreedSalesParty[position] != null ? "" + agreedSalesParty[position] : "");
holder.etAgreeMgr.setText(agreedSalesMgr[position] != null ? "" + agreedSalesMgr[position] : "");

答案 1 :(得分:0)

尝试这个,它可能会起作用:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = null;
    convertView=null;
    if (convertView == null) {
        vi = LayoutInflater.from(this.context).inflate(
                R.layout.row_item_edit, null);
        holder = new ViewHolder();
        .........
        ...........
        ..............
    }

return vi;
}

答案 2 :(得分:0)

还需要在设置文本中有其他部分

if (agreedVolume[holder.number] != null) {
        holder.etAgreed.setText("" + agreedVolume[position]);
    }else{
        holder.etAgreed.setText("");
    }

    if (agreedSalesParty[position] != null) {
        holder.etAgreedParty.setText("" + agreedSalesParty[position]);
    }else{
        holder.etAgreedParty.setText("");
    }

    if (agreedSalesMgr[position] != null) {
        holder.etAgreeMgr.setText("" + agreedSalesMgr[position]);
    }else{
        holder.etAgreeMgr.setText("");
    }