5

次のカスタム ベース アダプターがあります。各項目に編集テキストを含むリストビューがあります。ユーザーが編集テキストを離れるとすぐにそのデータを保存したいのですが、ユーザーが 1 文字を入力するたびに 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;

}

}

4

1 に答える 1

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;

        });

それがあなたを助けることを願っています。

于 2013-06-22T06:27:41.463 に答える