0

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

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

国家编号: 注册编号: 姓名:

234               34           john
2890              345          xxxx
21                3            smith

我将过滤后的值保存在数组列表中,并将该数组列表传递给适配器。

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));
            }
        }
    });
4

1 回答 1

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) {
}
}
于 2017-01-31T10:00:23.033 回答