TextWatcher 在 getView() 方法中不起作用
The TextWatcher is not working in the getView() method
一定有一些我忽略的简单的东西,或者我还不知道的东西。编译代码没有问题,Logcat.
也没有错误
我在另一个文件中使用了类似的 textWatcher 并且它可以工作,但是无论我将它放在 getView() 方法中的什么位置,都没有乐趣。
OnItemClickListener 正常工作,但当我在 exitText 框中键入内容时,textWatcher 不执行任何操作。
请详细回答。
谢谢。
在 .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.
您使用了错误的适配器。
您正在创建 carAdapter
:
final ArrayAdapter<String> carAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ALL_CARS);
但随后您将另一个(匿名)适配器分配给 ListView
:
carListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ALL_C){ ... }
您应该将 ListView
设置为使用 carAdapter
,如下所示:
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);
此外,您还要将另一个适配器声明为 class 成员
ArrayAdapter<String> listViewAdapter;
但从未使用过它。
我认为您应该将 carAdapter
声明为 class 成员。
修复非常简单,在 TextWatcher 中的一小行代码。
!!!一切快乐!!!
ListViews 不是我的强项。我根据我的问题对代码进行了一些重组,但任何人都应该很容易理解。
我花了数周时间进行搜索和测试,但现在我有了想要的东西。现在我可以完成我的项目了。
我希望这对可能正在尝试类似事情的其他人有所帮助。
感谢 Whosebug/StackExchange 的创建者,感谢 所有帮助过我的人。
在 TextWatcher 中放置适配器调用的功劳归功于 BruceHill 的答案 -
Filtering a ListView using an ArrayAdapter without overriding getFilter method
这是完整的代码:
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;
}
});
}
}
一定有一些我忽略的简单的东西,或者我还不知道的东西。编译代码没有问题,Logcat.
也没有错误我在另一个文件中使用了类似的 textWatcher 并且它可以工作,但是无论我将它放在 getView() 方法中的什么位置,都没有乐趣。
OnItemClickListener 正常工作,但当我在 exitText 框中键入内容时,textWatcher 不执行任何操作。
请详细回答。
谢谢。
在 .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.
您使用了错误的适配器。
您正在创建 carAdapter
:
final ArrayAdapter<String> carAdapter = new
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ALL_CARS);
但随后您将另一个(匿名)适配器分配给 ListView
:
carListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, ALL_C){ ... }
您应该将 ListView
设置为使用 carAdapter
,如下所示:
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);
此外,您还要将另一个适配器声明为 class 成员
ArrayAdapter<String> listViewAdapter;
但从未使用过它。
我认为您应该将 carAdapter
声明为 class 成员。
修复非常简单,在 TextWatcher 中的一小行代码。
!!!一切快乐!!!
ListViews 不是我的强项。我根据我的问题对代码进行了一些重组,但任何人都应该很容易理解。
我花了数周时间进行搜索和测试,但现在我有了想要的东西。现在我可以完成我的项目了。 我希望这对可能正在尝试类似事情的其他人有所帮助。
感谢 Whosebug/StackExchange 的创建者,感谢 所有帮助过我的人。
在 TextWatcher 中放置适配器调用的功劳归功于 BruceHill 的答案 -
Filtering a ListView using an ArrayAdapter without overriding getFilter method
这是完整的代码:
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;
}
});
}
}