edittext在滚动listview时丢失了它的内容

时间:2017-05-21 23:40:25

标签: android

我有listview的项目edittext,我有add_button作为页脚点击它应该添加新的edittext ..问题是当添加新行并填充数据时滚动listview它混淆数据或丢失edittext数据

当我添加新内容时添加超过3行时,添加的edittext将填充上一个数据

我怎么能解决我的问题 我尝试了几乎所有解决方案,但它不起作用 这是我的适配器

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 个答案:

答案 0 :(得分: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
.....

浏览此链接。 id_for_label

希望它有所帮助。