1

Please Help to Solve My Issue !!

In the Activity there is a listview and A Button

ListView Row contains a TextView which displays the Serial No and 3 Edittexts (in which values will be entered)

The button Adds another row to the List.

I used my own class MemberEntry.

Problem is that In listview, data is entered smoothly but on scrolling the textview value (Serial No.) remains fine but the value of edittext (All 3) changes(The value of some other Edittext from the list takes place of another one). I used this and found a little bit good results. But Not Accurate.

Code

Activity

OnCreate

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


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

OnClick of a button

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

Adapter

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>

I have already searched on google but not getting proper accurate Solution. Please Check My Code and Suggest best for me.

Ruchi
  • 21
  • 3
  • the name of the method is `addTextChangedListener` not `setTextChangedListener` (with all obiovus consequences) and it's called even if view is reused ... – Selvin Aug 19 '17 at 17:10
  • @Selvin Should I `addTextChangedListener` after `holder.ETDefaultShare = (EditText) row.findViewById(R.id.ETDefaultShare);` both times – Ruchi Aug 20 '17 at 03:28
  • Let the holder itself implement listener, set it only once - in constructor ... – Selvin Aug 20 '17 at 07:54
  • Post code and don't you downvote (I have met you earlier also) – Ruchi Aug 20 '17 at 08:15
  • So, I don't need if-else inside else – Ruchi Aug 20 '17 at 08:21
  • @Selvin pls post complete code.....if u want u can give your own and not edit mine – Ruchi Aug 20 '17 at 10:20
  • +1 for I have the same type of issue. Earlier I had duplicate entry somewhat related to screen size and now i too am facing the same issue. – Daksh Agrawal Aug 20 '17 at 11:05

0 Answers0