大家。所以,我的 TextWatcher 有问题。我的列表视图工作正常。当我单击此 ListView (lv_arr) 中的特定项目时,它会打开一个我想要的特定类,到目前为止一切都很好。但是,当我从字符串(列表视图)之一搜索项目(名称)而不是打开与名称对应的特定类时,我的文本观察器(我正在使用从列表视图中搜索项目的编辑文本 [布局]) item,它根据位置打开一个类。例如:我搜索“Fluke”,而不是打开 Fluke.Class,而是打开 BirdHand.class。那不是我想要的。我希望它打开 Fluke.class
这是我正在使用的代码。我是新手:
public class Searchsort extends Activity {
private ListView lv1;
private EditText ed;
private String lv_arr[]={
"a bird in the hand is worth two in the bush",
"a bolt from/out of the blue",
"a penny for your thoughts",
"fluke",
"have a face like thunder",
};
private ArrayList<String> arr_sort= new ArrayList<String>();
int textlength=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
lv1 = (ListView)findViewById(R.id.list);
ed = (EditText)findViewById(R.id.EditText01);
lv1.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
if ("a bolt from/out of the blue".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, BoltBlue.class);
startActivity(myIntent);
}
if ("a bird in the hand is worth two in the bush".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, BirdHand.class);
startActivity(myIntent);
}
if ("a penny for your thoughts".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, PennyThoughts.class);
startActivity(myIntent);
}
if ("fluke".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, Fluke.class);
startActivity(myIntent);
}
if ("have a face like thunder".equals(lv_arr[position])) {
Intent myIntent = new Intent(Searchsort.this, FaceThunder.class);
startActivity(myIntent);
}
}
});
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
ed.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=ed.getText().length();
arr_sort.clear();
for(int i=0;i<lv_arr.length;i++) {
if(textlength<=lv_arr[i].length()) {
if(ed.getText().toString().equalsIgnoreCase((String) lv_arr[i].subSequence(0, textlength))) {
arr_sort.add(lv_arr[i]);
}
}
}
lv1.setAdapter(new ArrayAdapter<String> (Searchsort.this,android.R.layout.simple_list_item_1 , arr_sort));
}
});
}
}