setOnFocusChangeListener - 在每个字符后触发

时间:2013-03-15 09:02:15

标签: android listview android-edittext baseadapter

我有以下自定义基本适配器。我有一个listview,每个项目都有一个edittext。我想在用户离开edittext时立即保存该数据,但现在发生的是每次用户键入1个char时,setOnFocusChangeListener会触发。我真的不知道为什么会这样。

我的自定义基础适配器类

public class ChecklistBaseAdapter extends BaseAdapter {
private static ArrayList<Checklist> searchArrayList;
Context currentcontext;
DatabaseHandler db;
private LayoutInflater mInflater;

public ChecklistBaseAdapter(Context context, ArrayList<Checklist> results) {

    searchArrayList = results;
    mInflater = LayoutInflater.from(context);
    currentcontext = context;
    db = new DatabaseHandler(currentcontext);
}

public int getCount() {

    return searchArrayList.size();
}

public void remove(int position) {

    searchArrayList.remove(position);
    notifyDataSetChanged();
}

public void removeAll() {

    searchArrayList.clear();
    notifyDataSetChanged();
}

public void add(Checklist checklist) {
    searchArrayList.add(checklist);

    notifyDataSetChanged();
}

public void notifyChange() {
    notifyDataSetChanged();
}

public Object getItem(int position) {

    return searchArrayList.get(position);

}

public long getItemId(int position) {

    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.checklistitem, null);
        holder = new ViewHolder();

        holder.cbStatus = (CheckBox) convertView
                .findViewById(R.id.checkbox);
        holder.etName = (EditText) convertView.findViewById(R.id.editname);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Checklist checklist = (Checklist) getItem(position);

    holder.etName.setText(checklist.getName());

    holder.etName.setId(position);
    if (searchArrayList.get(position).getStatus().equals("F")) {
        holder.cbStatus.setChecked(false);
    } else {
        holder.cbStatus.setChecked(true);
    }

    holder.etName.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                KeyEvent event) {

            if (actionId == EditorInfo.IME_ACTION_DONE) {
                int myint = v.getId();
                searchArrayList.get(myint)._name = v.getText().toString();
                return true;

            }
            return false;
        }
    });
    holder.etName
            .setOnFocusChangeListener(new OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    String input;
                    EditText editText;

                    if (!hasFocus) {
                        Log.e("test", "test123");
                        int myint = v.getId();
                        editText = (EditText) v;
                        input = editText.getText().toString();
                        searchArrayList.get(myint)._name = input;

                    }
                }
            });

    holder.cbStatus
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    String name = holder.etName.getText().toString().trim();
                    if (isChecked) {
                        for (int x = 0; x < searchArrayList.size(); x++) {
                            Checklist checklist = new Checklist();
                            checklist = searchArrayList.get(x);
                            if (name.equals(checklist.getName())) {
                                checklist.setStatus("T");
                                db.updateCheckList(checklist);
                                searchArrayList.set(x, checklist);
                            }
                        }
                    } else {
                        for (int x = 0; x < searchArrayList.size(); x++) {
                            Checklist checklist = new Checklist();
                            checklist = searchArrayList.get(x);
                            if (name.equals(checklist.getName())) {
                                checklist.setStatus("F");
                                searchArrayList.set(x, checklist);
                                db.updateCheckList(checklist);
                            }
                        }
                    }
                }
            });

    return convertView;
}

static class ViewHolder {

    CheckBox cbStatus;
    EditText etName;

}

}

1 个答案:

答案 0 :(得分:0)

尝试使用addTextChangedListener()方法

holder.etName.addTextChangedListener(new TextWatcher() {

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

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                  String input;
                EditText editText;

                    Log.e("test", "test123");
                    int myint = v.getId();
                    editText = (EditText)v;
                    input = editText.getText().toString();
                    searchArrayList.get(myint)._name = input;

        });

希望它会帮助你。