Должно быть что-то простое, что я пропускаю, или что-то, чего я просто еще не знаю.Нет проблем с компиляцией кода, и в Logcat нет ошибок.
Я использовал аналогичный textWatcher в другом файле, и он работает, но независимо от того, где я поместил его в метод getView (),без радости.
OnItemClickListener работает так, как должен, но textWatcher ничего не делает, когда я печатаю в поле exitText.
Пожалуйста, будьте многословны с вашим ответом.
Спасибо.
В файле .xml :
<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" />
Полный код:
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.