-1

I have listview with items edittext and i have add_button as footer when click on it it should add new edittext .. the problem is when adding new row and fill it with data when scroll listview it confuse the data or lose the edittext data

and also when adding more than 3 rows when i add new the edittext added is fill with the previouse data

how can i solve my isuue please I tried almost all solutions and it doesn't work this is my adapter

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;


import java.util.ArrayList;


public class DevicesAdapter extends BaseAdapter {

private Context context;
private static ArrayList<Device> devices;
private LayoutInflater layoutInflater;

public DevicesAdapter(Context context) {
    this.context = context;
    devices = new ArrayList<Device>();
    layoutInflater = LayoutInflater.from(context);
}

public void setContext(Context context) {
    this.context = context;
}

public void setPosts(ArrayList<Device> posts) {
    this.devices = posts;
}

public void addPost(Device device) {
    //add post to the list
    devices.add(device);
    //notify the adapter to change the view
    notifyDataSetChanged();
}

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

@Override
public Device getItem(int i) {
    return devices.get(i);
}

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

// our ViewHolder.

static class ViewHolderItem {
    EditText energy;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Log.e("position item",position+"");
    ViewHolderItem viewHolder;

    if(convertView==null){

        // well set up the ViewHolder
        viewHolder = new ViewHolderItem();
        // inflate the layout
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.device_item,parent,false);

        viewHolder.energy = (EditText) convertView.findViewById(R.id.eneregy_edittext);



        convertView.setTag(viewHolder);
    }else{
        viewHolder = (ViewHolderItem) convertView.getTag();
    }

    viewHolder.energy.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) {
            if (s.length()!=0)
                devices.get(position).setEnergy(Integer.parseInt(String.valueOf(s)));
        }
    });

    return convertView;
}

public static ArrayList<Device> getDevices() {
    return devices;

}

public void clear() {
    devices.clear();
    notifyDataSetChanged();
}

public int getViewTypeCount() {
    return getCount();
}

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

1 Answers1

0
.....

else{
        viewHolder = (ViewHolderItem) convertView.getTag();
    }

//check for nul if required
holder.energy.setText(devices.get(position).getEnergy());  // add this line 


    viewHolder.energy.addTextChangedListener(new TextWatcher() {
        @Override
.....

Go through this link. EditText loses content on scroll in ListView

Hope it helps.

Community
  • 1
  • 1