在滚动时更改的ListView EditText值也采用重复条目

时间:2017-08-19 16:48:17

标签: android listview scroll android-edittext recycle

请帮助解决我的问题!!

在活动中有一个列表视图和一个按钮

ListView Row包含一个TextView,它显示Serial No和3 Edittexts(其中将输入值)

该按钮向List添加另一行。

我使用了自己的类MemberEntry。

问题是在列表视图中,数据输入顺畅但滚动时输入 textview值(序列号)仍然很好但是 edittext(全部3)更改的值(列表中某些其他Edittext的值取代另一个)。 我使用this并发现了一些不错的结果。但不准确。

代码

活动

的OnCreate

 MemberEntry m = new MemberEntry();
 memberEntries = new ArrayList<>();
 memberEntries.add(m);


 adapter = new CustomListViewAdapter3(this, R.layout.row_entry_member, memberEntries);
 LVMembers.setAdapter(adapter);

点击按钮

public void fab(View view) {
    memberEntries.add(new MemberEntry());//memberEntries and adapter are global
    adapter.notifyDataSetChanged();
}

适配器

public class CustomListViewAdapter3 extends ArrayAdapter<MemberEntry> {
Context context;
int layoutResourceId;
public String[] Current;
ArrayList<MemberEntry> data = new ArrayList<>();
public static HashMap<Integer,String> myListOfNames=new HashMap<Integer,String>();


public CustomListViewAdapter3(Context context, int layoutResourceId,
                              ArrayList<MemberEntry> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    Current=new String[50];
    this.data = data;
    Log.d("CustomListViewAdapter", "Data = " + data + "\n Data Length = " + data.size() + "\n getCount=" + getCount());
    for(int i=0;i<50;i++)
    {
        myListOfNames.put(i,"");
    }
}

@Override
public int getCount() {
    if(data != null && data.size() != 0){
        return data.size();
    }
    return 0;
}
@Override
public MemberEntry getItem(int position) {
    // TODO Auto-generated method stub
    return data.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public boolean hasStableIds() {
    return true;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public int getItemViewType(int position) {

    return position;
}

@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
    View row = convertView;
    final RecordHolder holder;
    //d("CustomListViewAdapter","GetView() called");
    MemberEntry m = data.get(position);
    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RecordHolder();
        holder.TVMemNo = (TextView) row.findViewById(R.id.TVMemNo);
        holder.ETName = (EditText) row.findViewById(R.id.ETName);
        holder.ETMobileNo = (EditText) row.findViewById(R.id.ETMobile);
        holder.ETDefaultShare = (EditText) row.findViewById(R.id.ETDefaultShare);
        row.setTag(holder);
        m.setBinded(true);
        Log.w("Yo","M is now Binded");
        //e("Row is NOT Null", "NOT NULL  NOT NULL NOT NULL");
    } else {
        if(m.isBinded()) {
            Log.w("Yo","M found Binded");
            holder = (RecordHolder) row.getTag();
        }else{
            Log.w("Yo","M found unBinded");
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new RecordHolder();
            holder.TVMemNo = (TextView) row.findViewById(R.id.TVMemNo);
            holder.ETName = (EditText) row.findViewById(R.id.ETName);
            holder.ETMobileNo = (EditText) row.findViewById(R.id.ETMobile);
            holder.ETDefaultShare = (EditText) row.findViewById(R.id.ETDefaultShare);
            row.setTag(holder);
            Log.w("Yo","M is now Binded");
            m.setBinded(true);
        }
        //e("Row is Null","NULL NULL NULL");
    }

    holder.ref = position;
    holder.TVMemNo.setText("+ Add Member " + (position + 2));//don't think much about +2, answer well and i will give a +2 (from my friend's account)
    holder.ETName.addTextChangedListener(new TextWatcher() {

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

        }

        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
            // TODO Auto-generated method stub

        }

        public void afterTextChanged(Editable s) {

            Current[holder.ref] = s.toString();
            myListOfNames.put(position,s.toString().trim());
        }
    });
    holder.ETName.setText(myListOfNames.get(position));
    m.setRow(row);
    data.set(position, m);
    Log.d("Hi", "Added A Row");
    return row;
}

//Log.e("Logger", "Position=" + position);


public ArrayList<MemberEntry> getEntries() {
    return data;
}

static class RecordHolder {
    //TextView TVRequestSent;
    TextView TVMemNo;
    EditText ETName, ETMobileNo, ETDefaultShare;
    int ref;
}


}

row_entry_member.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="5dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_weight=".3"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/TVMemNo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="5dp"
            android:layout_weight="1.6"
            android:gravity="left"
            android:text="+ Add Member 2"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#4285F4"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView96"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"
            android:gravity="right"
            android:text="(if Require)  Default Share %"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#4285F4" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="10dp"
        android:layout_weight="1.8"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/textInpuitLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/textInputLayout"
            android:layout_weight="1">

            <EditText
                android:id="@+id/ETName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="left"
                android:hint="Name"
                android:inputType="textPersonName"
                android:maxLength="15"
                android:textColor="#33691e" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/texjtInpuitLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/textInputLayout"
            android:layout_weight="1.2">

            <EditText
                android:id="@+id/ETMobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="left"
                android:hint="Mobile No."
                android:inputType="number"
                android:maxLength="10"
                android:textColor="#33691e" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/textInpjuitLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/textInputLayout"
            android:layout_weight="1.6">

            <EditText
                android:id="@+id/ETDefaultShare"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="15dp"
                android:layout_weight="1.4"
                android:gravity="center"
                android:hint=" %"
                android:inputType="number"
                android:maxLength="2"
                android:textColor="#33691e" />
        </android.support.design.widget.TextInputLayout>

    </LinearLayout>
</LinearLayout>

</LinearLayout>

我已经在谷歌搜索但没有得到正确的解决方案。 请检查我的代码并建议我最好。

0 个答案:

没有答案