0

みんな。だから、私は TextWatcher に問題があります。私のリストビューは正常に動作します。この ListView (lv_arr) から特定の項目をクリックすると、必要な特定のクラスが開きます。しかし、私のテキストウォッチャー(リストビューからアイテムを検索する編集テキスト[レイアウト]を使用しています)の名前に対応する特定のクラスを開く代わりに、文字列(リストビュー)の1つからアイテム(名前)を検索するとアイテム、それは位置に応じてクラスを開きます。例: "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 に答える