3个edittext字段的文本观察器 - 我需要根据3个编辑文本字段过滤结果

时间:2012-11-30 09:14:15

标签: android textwatcher

我有3个编辑文本字段。这三个人都使用了文本观察器。它运作良好。这意味着,如果我在edittext A中输入内容,它会在edittext B和edittext C中提供基于A.的结果。我想得到合并结果。我想像这样过滤结果:

如果我在国家号码中键入“2”并在注册号码中键入“3”,结果应该是这样的

国家号码:登记号码:姓名:

234               34           john
2890              345          xxxx
21                3            smith

我将过滤后的值保存在arraylist中并将该arraylist传递给适配器。

edtNationalityInfo.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        }

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

        }

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

            textlength = edtNationalityInfo.getText().length();

            if (textlength > 0)
                nationalFlag = true;
            else
                nationalFlag = false;

            // filtered_data.clear();
            for (int i = 0; i < data.size(); i++) {
                if (textlength <= data.get(i).get_nationalityno().length()) {
                    if (edtNationalityInfo
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) data.get(i)
                                            .get_nationalityno()
                                            .subSequence(0, textlength))) {

                        filtered_data.add(data.get(i));
                    }
                }
            }

            lvVotersList.setAdapter(new TextWatcherAdapter(Attendance.this,
                    filtered_data));

        }
    });


edtVoterName.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        }

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

        }

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

            textlength = edtVoterName.getText().length();
            if (textlength > 0)
                nameFlag = true;
            else
                nameFlag = false;

            // filtered_data.clear();
            for (int i = 0; i < data.size(); i++) {
                if (textlength <= data.get(i).get_name().length()) {
                    if (edtVoterName
                            .getText()
                            .toString()
                            .equalsIgnoreCase(
                                    (String) data.get(i).get_name()
                                            .subSequence(0, textlength))) {
                        filtered_data.add(data.get(i));
                    }
                }
            }

            lvVotersList.setAdapter(new TextWatcherAdapter(Attendance.this,
                    filtered_data));
        }
    });

这是我的ArrayList:

data = new ArrayList<DataAttendance>();
filtered_data = new ArrayList<DataAttendance>();

//我的数据模型类:

public class DataAttendance {

public String _id = null;
public String _serialno = null;
public String _nationalityno = null;
public String _voteCasted = null;
public String _voteCastedTime = null;
public String _name = null;


public DataAttendance() {

}

public DataAttendance(String _name, String _serialno,
        String _nationalityno, String _voteCasted, String _voteCastedTime) {
    this._name = _name;
    this._nationalityno = _nationalityno;
    this._serialno = _serialno;
    this._voteCasted = _voteCasted;
    this._voteCastedTime = _voteCastedTime;
}



public String get_id() {
    return _id;
}

public void set_id(String _id) {
    this._id = _id;
}

public String get_name() {
    return _name;
}

public void set_name(String _name) {
    this._name = _name;
}

public String get_nationalityno() {
    return _nationalityno;
}

public void set_nationalityno(String _nationalityno) {
    this._nationalityno = _nationalityno;
}

public String get_serialno() {
    return _serialno;
}

public void set_serialno(String _serialno) {
    this._serialno = _serialno;
}

public String get_voteCasted() {
    return _voteCasted;
}

public void set_voteCasted(String _voteCasted) {
    this._voteCasted = _voteCasted;
}

public String get_voteCastedTime() {
    return _voteCastedTime;
}

public void set_voteCastedTime(String _voteCastedTime) {
    this._voteCastedTime = _voteCastedTime;
}

现在我变得像:

如果我在国家号码中键入“2”并在注册号码中键入“3”,结果应该是这样的

国家号码:登记号码:姓名:

234               434          siva
2890              345          elma
21                23           laura 
像这样......

希望我能在这里得到帮助..!

// --------------------------------------------- ---------------------------------------

我有另一个数组列表的解决方案:

这是:

它可能对某人有帮助..

    edtVoterName.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

        }

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

        }

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

            int Votertextlength = edtVoterName.getText().length();
            if (Votertextlength > 0) {
                nameFlag = true;
            } else {
                nameFlag = false;
            }

            textlength = edtVoterName.getText().length();

            if (nationalFlag == true || registerFlag == true) {

                Log.d("Attendance Flag : ",
                        " The Attendance nationalFlag == " + nationalFlag
                                + "  registerFlag ==  : " + registerFlag);

                ArrayList<DataAttendance> temp_data;
                temp_data = new ArrayList<DataAttendance>();
                textlength = edtVoterName.getText().length();
                // filtered_data.clear();
                for (int i = 0; i < filtered_data.size(); i++) {
                    if (textlength <= filtered_data.get(i).get_name()
                            .length()) {
                        if (edtVoterName
                                .getText()
                                .toString()
                                .equalsIgnoreCase(
                                        (String) filtered_data.get(i)
                                                .get_name()
                                                .subSequence(0, textlength))) {
                            temp_data.add(filtered_data.get(i));
                        }
                    }
                }
                filtered_data = temp_data;
                lvVotersList.setAdapter(new TextWatcherAdapter(
                        Attendance.this, filtered_data));
            } else {
                filtered_data.clear();
                for (int i = 0; i < data.size(); i++) {
                    if (textlength <= data.get(i).get_name()
                            .length()) {
                        if (edtVoterName
                                .getText()
                                .toString()
                                .equalsIgnoreCase(
                                        (String) data.get(i)
                                                 .get_name()
                                                .subSequence(0, textlength))) {

                            filtered_data.add(data.get(i));
                        }
                    }
                }

                lvVotersList.setAdapter(new TextWatcherAdapter(
                        Attendance.this, filtered_data));
            }
        }
    });

1 个答案:

答案 0 :(得分:0)

使用此

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

EditText e = new findViewById(R.id.editText);
EditText e1 = new findViewById(R.id.editText1);
EditText e2 = new findViewById(R.id.editText2);

e.addTextChangedListener(new CustomTextWatcher(e));
e.addTextChangedListener(new CustomTextWatcher(e1));
e.addTextChangedListener(new CustomTextWatcher(e2));
}

private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;

public CustomTextWatcher(EditText e) { 
    mEditText = e;
}

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

public void onTextChanged(CharSequence s, int start, int before, int count) {  
if(mEditText.getId()== R.id.editText){}
else if(mEditText.getId()== R.id.editText2){}
else{}
}

public void afterTextChanged(Editable s) {
}
}