我无法打开我想要的正确课程

时间:2012-07-31 18:06:16

标签: android listview search textwatcher

大家。所以,我的TextWatcher有问题。我的listview工作正常。当我单击此ListView(lv_arr)中的特定项时,它会打开我想要的特定类,到目前为止一切都很好。但是,当我从一个字符串(listview)中搜索一个项目(名称)而不是打开一个对应于该名称的特定类时,我的textwatcher(我正在使用编辑文本[layout]来搜索listview中的项目) 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));

        }
    });
}

}

1 个答案:

答案 0 :(得分:1)

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

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

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

希望你理解我的解释。