View Lists as an Android List Editing Text Values ​​When Scrolling

I have listviewwith one textviewand a edit textin each row. If I set the value in one edit text box, and then when scrolling down, I get a duplicate value for this entered field value in another edit text box. How can I solve this problem?

My adapter class is shown below:

public class CustomAdapter extends ArrayAdapter<String> {
  private Context       context;
  private String[]  names;
  ViewHolder holder;

  static class ViewHolder {
    public TextView text;
    public EditText editText;
  }

  public CustomAdapter(Context context, int textViewResourceId, String[] names) {
    super(context, textViewResourceId, names);
    this.context = context;
    this.names = names;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_design, parent, false);
        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.textView1);
        holder.editText = (EditText) convertView.findViewById(R.id.editText1);
        convertView.setTag(holder);         
    } else {            
        holder = (ViewHolder) convertView.getTag();
    }

    String s = names[position];
    holder.text.setText(s);
    return convertView;
  }
}

I referred to these links: Listview duplication actions , Android listview duplicates scroll element

+4
source share
4 answers

-, Listview. , , ,

if(convertview == null)
{
 //code here 
}else {
 //code here 
}    

, , :

 @Override
  public int getItemViewType(int position) {
    return position;
}

@Override
public int getViewTypeCount() {
    return 500;
}
+5

:

public class CustomAdapter extends ArrayAdapter<String> {
  private Context       context;
  private String[]  names;

  static class ViewHolder {
    public TextView text;
    public EditText editText;
  }

  public CustomAdapter(Context context, int textViewResourceId, String[] names) {
    super(context, textViewResourceId, names);
   this.context = context;
    this.names = names;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.row_design, parent, null);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) rowView.findViewById(R.id.textView1);
        viewHolder.editText = (EditText) rowView.findViewById(R.id.editText1);
        rowView.setTag(viewHolder);         
    }          
        ViewHolder holder = (ViewHolder)rowView.getTag();


    String s = names[position];
    holder.text.setText(s);
    return rowView;
  }
}

. .

, .

0

, . . , /.

, , ListView .

, List , .

List<String> names;
List<String> edit;

// In Constructor()
this.names = names;
// Create a list with empty data for the editTexts
int listSize = names.size();
String emptyString = new String("");
List<String> emptyList = new ArrayList<String>(listSize);
for(int i = 0; i < listSize; i ++){
    emptyList.add(emptyString);
}
this.edit = emptyList;

// In getView()
// add watcher to editText and when it is changed, update the value in 'edit'
// Simply set the corresponding values to this row to the editText
holder.text.setText(names.get(position));
holder.editText.setText(edit.get(position));

, .

, fooobar.com/questions/1508685/...

0

Update:

, ViewHolder, HailNaRoz. HailNaRoz , , ViewHolder View , Google Android Dev. ViewHolder Views (EditText, TextView, ImageView ..) , .

RecyclerView, , . RecyclerView , ​​ gradle

. (Recycler View)

:

, EditText . TextView , , EditText.

, EditText reset getView().

TextWatcher if/else getView():

holder.editText.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) {
            }

            public void afterTextChanged(Editable s) {
                  editTextList.put(position,s.toString());
            }
        });

HashMap, . position int getView(). .

HashMap:

private HashMap<Integer,String> editTextList =new HashMap<Integer,String>();

, TextView, EditText:

holder.text.setText(s);
holder.editText.setText(editTextList.get(position));

This should contain the current position EditTextin the correct field EditText.

0
source

All Articles