The TextWatcher is not working in the getView() method

170 Views Asked by At

There must be something simple that I am overlooking, or something I just don't know yet. There is no problem compiling the code, and there are no errors in the Logcat.

I have used a similar textWatcher in another file and it works, but regardless of where I put it in a getView() method, no joy.

The OnItemClickListener works like it should, but the textWatcher does nothing when I type in the exitText box.

Please be verbose with your answer.

Thank you.

In the .xml file:

<EditText
    android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="Type A Car Name Here"
    android:inputType="textPersonName"
    android:textColorHint="@color/colorPrimary"
    android:textSize="20sp" />

The complete code:

package abc.AvailableCars;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class CarListActivity extends Activity {

private ListView carListView;

ArrayAdapter<String> listViewAdapter;

ArrayList<HashMap<String, String>> CarList;

EditText editTextSearch;

EditText editTextCarAmount;

 Integer cListviewPosition = 0;

 int cLitemPosition = 0;

 String cLRowContent;

 String cLItemContent;

 String[] ALL_CARS;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.car_list_layout);

carListView = (ListView) findViewById(R.id.list_view);

editTextSearch = (EditText) findViewById(R.id.inputSearch);
editTextSearch.setSingleLine();

editTextCarAmount = (EditText) findViewById(R.id.carAmount);
editTextCarAmount.setSingleLine();

ALL_CARS = new String[]{"European Cars:", "Mercedes", 
"Passat", "Bently", "Porsche", "BMW", "Yugo", "Land 
Rover", "Japanese Cars:", "Maxima GXE", "Mazda6", 
"Avalon", "Toyota", "Honda", ""};

final ArrayAdapter<String> carAdapter = new 
ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, ALL_CARS);

carListView.setAdapter(new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, ALL_CARS) {

@Override
public View getView(int rowPosition, View convertView, ViewGroup parent) {

View row = super.getView(rowPosition, convertView, parent);

if(getItem(rowPosition).equals("European Cars:") || getItem(rowPosition).equals("Japanese Cars:")) {


row.setBackgroundColor(Color.parseColor("#B7B7B7"));

}  // both of the getItems end here.

else {

row.setBackgroundColor(Color.parseColor("#EEE8AA"));

} // else ends here.


// Enable the search filter:
editTextSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence charSeq, int arg1, int arg2, int arg3) {

// Search for each letter typed in the editText:

carAdapter.getFilter().filter(charSeq);

}  // onTextChanged ends here.


@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub


}  // beforeTextChanged ends here.

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub


}  // afterTextChanged ends here.

}  // addTextChangedListener ends here.

);  // TextWatcher ends here.

carListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

// The ListView Row That Was Clicked
cLitemPosition = position;

// The Content Of The ListView Row That Was Clicked
cLItemContent = (String) carListView.getItemAtPosition(position);

cListviewPosition = cLitemPosition;
cLRowContent = cLItemContent;

editTextSearch.setText("");
editTextSearch.setText(cLRowContent);

}  // position ends here.

}  // setOnItemClickListener ends here.

    );  // AdapterView.OnItemClickListener ends here.

    return row;

    }  // getView ends here.

    });  // carListView.setAdapter ends here.

    }  // onCreate ends here.

}  // CarListActivity ends here.
2

There are 2 best solutions below

0
Krang On BEST ANSWER

The fix was something very simple, one short line of code inside of the TextWatcher.
!!! ALL JOY !!!

ListViews are NOT my forte. I restructured the code a little from my question, but it should be easy enough for anyone to figure out.

It took weeks of searching and testing, but now I have what I wanted. Now I can finish my projects. I hope this helps anyone else who may be attempting something similar.

THANK YOU to the creator(s) of StackOverflow/StackExchange, and THANK YOU to everyone who has helped me.

The credit for placing the adapter call inside of the TextWatcher goes to BruceHill for the answer at-

Filtering a ListView using an ArrayAdapter without overriding getFilter method

This is the complete code:

package abc.AvailableCars;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class CarListActivity extends Activity {
private ListView carListView;

ArrayAdapter<String> carAdapter;

// the ArrayList for the Listview
ArrayList<HashMap<String, String>> CarList;

EditText editTextSearch;

EditText editTextCarAmount;

Integer cListviewPosition = 0;

int cLitemPosition = 0;

String cLRowContent;

String cLItemContent;

String[] ALL_CARS;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.car_list_layout);

carListView = (ListView) findViewById(R.id.list_view);

editTextSearch = (EditText) findViewById(R.id.inputSearch);
editTextSearch.setSingleLine();

editTextCarAmount = (EditText) findViewById(R.id.carAmount);
editTextCarAmount.setSingleLine();

ALL_CARS = new String[]{"European Cars:", "Mercedes", "Passat", "Bently", "Porsche", "BMW", "Yugo", "Land Rover",
"Japanese Cars:", "Maxima GXE", "Mazda6", "Avalon", "Toyota", "Honda", ""};

carAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ALL_CARS);
carListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ALL_CARS) {

    @Override
    public View getView(int rowPosition, View convertView, ViewGroup parent) {

    View row = super.getView(rowPosition, convertView, parent);

    if(getItem(rowPosition).equals("European Cars:") || getItem(rowPosition).equals("Japanese Cars:")) {

    row.setBackgroundColor(Color.parseColor("#B7B7B7"));
    }

    else {

    row.setBackgroundColor(Color.parseColor("#EEE8AA"));

    }

editTextSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence charSeq, int arg1, int arg2, int arg3) {

carAdapter.getFilter().filter(charSeq);

**// Without this line, HERE, the TextWatcher would not work inside of the getView() method.**
**carListView.setAdapter(carAdapter);**

carListView.setBackgroundColor(Color.parseColor("#EEE8AA"));

}

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

}

@Override
public void afterTextChanged(Editable arg0) {

}

}

);

carListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

cLitemPosition = position;

cLItemContent = (String) carListView.getItemAtPosition(position);

cListviewPosition = cLitemPosition;
cLRowContent = cLItemContent;


if (cLRowContent.equals("European Cars:") || cLRowContent.equals("Japanese Cars:")) {

return;

}

editTextSearch.setText("");
editTextSearch.setText(cLRowContent);

}

}
    );

    return row;

    }

    });

    }

}
2
Magnus On

You're using the wrong adapter.

You're creating carAdapter:

final ArrayAdapter<String> carAdapter = new
    ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, ALL_CARS);

but then you're assigning another (anonymous) adapter to the ListView:

carListView.setAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, ALL_C){ ... }

You should set the ListView to use carAdapter, like this:

final ArrayAdapter<String> carAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ALL_CARS) {

    @Override
    public View getView(int rowPosition, View convertView, ViewGroup parent) {
        ...
    }
};

carListView.setAdapter(carAdapter);

Furthermore, you're declaring yet another adapter as a class member

ArrayAdapter<String> listViewAdapter;

but never using it.

I think you should declare carAdapter as a class member instead.