Simon W. Simon W. - 3 years ago 109
Android Question

Android ArrayAdapter Filter not working properly

I have a problem using a filter with ArrayAdapter.

I have a ListView that gets published with data. Above the ListView there is an EditText Field which has a TextChangeListener on it. When entering a char sequence which definitely occurs in the ListView, under some circumstances the ListView stays empty afterwards. The elements in the ListView are formatted like this:

FFI 01 Jacket
Test, User
Locker 101
----------------
FFI 01 Helmet
Test, User
Locker 101
----------------
FFI 02 Jacket
Test, User2
Locker 102
----------------
.
.
.


and so on.


  • If I search for "FFI", "01", "02", "Jacket" or "Helmet", only items with the filtered text in it are left.

  • If I search for "User", "User2", "101" or "102", only items with the filtered text in it are left.

  • If I seach for "Test" or "Locker", the ListView is empty.



Here is my Code:

TextChangeListener:

eText.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
if (!cs.equals(null) || !cs.equals("") || !cs.equals(" ")) {
MainActivity.this.adapter.getFilter().filter(cs);
}
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }

@Override
public void afterTextChanged(Editable arg0) {}
});


Populating Adapter and ListView:

File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + "AUSRUEST.DBF");
DbfIterator dbfIterator = DbfEngine.getReader(file, null);
DbfHeader dbfHeader = DbfEngine.getHeader(file, null);
int numCols = dbfHeader.getCountColumns();
int numRows = dbfHeader.getCountRecords();
arrayListView = new String[numRows][numCols];
int i=0;
Set<String> set = new HashSet<String>();
while (dbfIterator.hasMoreRecords()) {
DbfRecord dbfRecord = dbfIterator.nextRecord();

if (dbfRecord.getString("FW_NR") != null && !dbfRecord.getString("FW_NR").startsWith("?")) {
String setText = dbfRecord.getString("FW_NR")+"\n"+dbfRecord.getString("STANDORT1")+"\n"+dbfRecord.getString("ABLAGE")+"\n";
setText = setText.replace("null", "---");
set.add(setText);
}

arrayListView[i][0] = dbfRecord.getString("GRUPPE2");
arrayListView[i][1] = dbfRecord.getString("FW_NR");
arrayListView[i][2] = dbfRecord.getString("STANDORT1");
arrayListView[i][3] = dbfRecord.getString("ABLAGE");
//Date date = dbfRecord.getDate("date_val");
String test = arrayListView[i][0];
String test1 = arrayListView[i][1];
String test2 = arrayListView[i][2];
String test3 = arrayListView[i][3];
System.out.println(arrayListView[i][0] + " " + arrayListView[i][1] + " " + arrayListView[i][2] + " "+ arrayListView[i][3]);
i++;
}
List<String> al = new ArrayList<String>(set);
Collections.sort(al);
System.out.println(" ");
ListView lv = (ListView) findViewById(R.id.listView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, al);
lv.setAdapter(adapter);

Answer Source

Replace "\n" dbfRecord.getString("FW_NR")+"\n"+dbfRecord.getString("STAND‌​ORT1")+"\n"+dbfRecor‌​d.getString("ABLAGE"‌​)+"\n" with "\n " and try.

Answer by Nigam Patro

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download