使用addchangelistener的Android适配器的edittext

时间:2014-07-16 12:34:41

标签: android android-edittext

基本上在尝试了一整天后,我似乎仍然无法让听众正确。我在这里要做的是使用监听器从Edittext中捕获一些信息;实现。但是随着代码的附加,我似乎无法捕获正确的信息。无论行如何,每当更改Edittext时,只有第一行会“更改”。

请劝告。

public class ModuleAdapterCom extends ArrayAdapter<moduleDetails> {


Context context;
int layoutResourceId;
ArrayList<moduleDetails> data = new ArrayList<moduleDetails>();


public ModuleAdapterCom(Context context, int layoutResourceId, ArrayList<moduleDetails> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new UserHolder();
        holder.comName = (EditText) row.findViewById(R.id.comNameEditText);
        holder.weightage = (EditText) row.findViewById(R.id.weightageEditText2);
        holder.minTargetEditText = (EditText) row.findViewById(R.id.minTargetEditText);
        holder.watcher = new myWatcher();
        holder.achieved = (EditText) row.findViewById(R.id.achieved);
        holder.achieved.addTextChangedListener(holder.watcher);
        holder.watcher.setActive(false);
        row.setTag(holder);
    } else {
        holder = (UserHolder) row.getTag();
    }
    moduleDetails selected = data.get(position);
    holder.comName.setText(selected.getName());
    holder.weightage.setText(""+selected.getComponentPercent());
    holder.minTargetEditText.setText(""+ selected.getMinPercent());

    if (data.get(position).getAchievePercent() == null){holder.watcher.setActive(true);}
    else {
        holder.watcher.setActive(false);
        holder.achieved.setText(""+selected.getAchievePercent());
        holder.watcher.setActive(true);
      /*  System.out.println("Setting: " + data.get(position).getName()+ " to: "+
                data.get(position).getAchievePercent());

     System.out.println("Setting: " + data.get(1).getName()+ " to: "+
              data.get(1).getAchievePercent()); */
        ;}

    return row;

}

static class UserHolder {
    EditText comName;
    EditText weightage;
    EditText minTargetEditText;
    EditText achieved;
    myWatcher watcher;

}

class myWatcher implements TextWatcher {

    private int mPosition;
    private boolean mActive;

    void setPosition(int position) {
        mPosition = position;
    }

    void setActive(boolean active) {
        mActive = active;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

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

        if (mActive) {

            data.get(mPosition).setAchievePercent(Double.parseDouble(s.toString()));
            System.out.println("managed to change "+ data.get(mPosition).getName()+"  "+
                    data.get(mPosition).getAchievePercent().toString());



        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

}
}

1 个答案:

答案 0 :(得分:1)

在您的

中添加notifyDataSetChanged()
@Override
public void afterTextChanged(Editable s) {

}

通过这种方式告诉适配器适配器中的当前数据已更新,以便反映列表视图中的更改。