0

大家。所以,我的 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));

        }
    });
}

}

4

1 回答 1

1

您正在修改 arr_sort 但与 lv_arr 进行比较。

例如,当用户搜索“fluke”并单击第一个位置时,该位置为 0,因此,应用程序比较 lv_arr 的位置 #0 并打开 BoltBlue.class 活动。

您应该与 arr_sort 进行比较,因为数组也必须更改。

希望你能理解我的解释。

于 2012-07-31T19:04:28.220 回答