2012-02-28 48 views
0

在使用文本觀察器搜索和顯示列表時遇到問題。 這是一個簡單的搜索操作在列表中。首先,數據是從.txt逐行檢索的。 字符串的第一個字是存儲在列表內容,現在我想在列表視圖Android:help Text Watcher,列表比較.logical error with afterTextChanged

這裏這個內容列表執行搜索操作並顯示結果是代碼

enter code here 

package com.android; 

import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.LineNumberReader; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.StringTokenizer; 

import android.app.ListActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.ListView; 
import android.widget.TextView; 

public class MyListDemoActivity extends ListActivity { 
    /** Called when the activity is first created. */ 
    TextView tv; 
    // final String[] values=new String[]{"android","Windows", 
    // "ios","BlackBerry","Java",".Net","WebOS", "Ubuntu", "Windows7"}; 
    //String[] content; 
    List<String> content ; 

    AutoCompleteTextView actv; 
    List<String> arr_sort; 
    ArrayAdapter<String> adapter; 
    ListView lv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     String line = " "; 
     LineNumberReader linenoreader = null; 
     StringTokenizer stringtokanixer = null; 
     content=new ArrayList<String>(); 
     lv=(ListView)findViewById(android.R.id.list); 

     try { 

      InputStream istream = getResources().openRawResource(R.raw.hihihi); 
      InputStreamReader streamreader = new InputStreamReader(istream); 

      linenoreader = new LineNumberReader(streamreader); 
      linenoreader.mark(15); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }// try catch ends here 



     Log.v("getting", "working"); 

     for (int i = 0; i < 6; i++) { 
      try { 
       line = linenoreader.readLine(); 
      } catch (IOException e) { 

       e.printStackTrace(); 
      } 
      Log.v("getting", line); 
      stringtokanixer = new StringTokenizer(line); 

      String st=stringtokanixer.nextToken(); 
      content.add(st);  
     }// for ends here 

        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,content); 
     //   setListAdapter(adapter); 

        lv.setAdapter(adapter); 
        tv= (TextView)findViewById(R.id.textView1); 



        actv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
        actv.addTextChangedListener(new TextWatcher() { 
         int len=0; 
         @Override 
         public void onTextChanged(CharSequence s, int start, int before, int count) { 
          // TODO Auto-generated method stub 

         } 

         @Override 
         public void beforeTextChanged(CharSequence s, int start, int count, 
           int after) { 
          // TODO Auto-generated method stub 

         } 

         @Override 
         public void afterTextChanged(Editable s) { 
          // TODO Auto-generated method stub 

          len=actv.getText().length(); 

          for(int i=0;i<content.size();i++) 
          { 
           if(len<=content.get(i).length()) 
           { 
            if(actv.getText().toString().equalsIgnoreCase((String) content.get(i).subSequence(0, len))) 
            { 

             arr_sort.add(content.get(i)); 

            } 


           } 

          } 

          adapter=new ArrayAdapter<String>(MyListDemoActivity.this,android.R.layout.simple_list_item_1,arr_sort); 
          setListAdapter(adapter); 
         } 
        }); // text watcher class ends here 



    }// on create ends here 
    public void onListItemClick(ListView ls,View v, int position,long id) { 
     tv.setText(content.get(position)); 

     //tv.setText(content[position]) // in case of string 

    }// endsd here onListItemClick(
} 

這裏是main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <AutoCompleteTextView 
     android:id="@+id/autoCompleteTextView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text=" " > 

     </AutoCompleteTextView> 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:drawSelectorOnTop="false" 
     > 
    </ListView> 

</LinearLayout> 

這裏是txt文件

joke happy 
hota happy okgame 
ok happy okgame 
asitis happy 
okgame happy 
oktested happy happy 

我想在afterTextChanged有邏輯錯誤,但無法弄清楚。

回答

0

解決

IM在冰淇淋sandwitch工作... 其與此代碼工作...

public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
        arr_sort = new ArrayList<String>(); 

        len = actv.getText().length(); 

        for (int i = 0; i < content.size(); i++) { 
         if (len <= content.get(i).length()) { 
          if (actv.getText() 
            .toString() 
            .trim() 
            .equalsIgnoreCase(
              (String) content.get(i).subSequence(0, 
                len))) { 

           arr_sort.add(content.get(i)); 
           Log.v("infor loop afterTextChanged", s.toString()); 
          } 

         } 

        } 

     //   adapter.notifyDataSetChanged(); 

        adapter = new ArrayAdapter<String>(MyListDemoActivity.this, 
         android.R.layout.simple_list_item_1, arr_sort); 
        setListAdapter(adapter); 


    } 
0

您不需要重新創建適配器。你應該修剪弦比較泰姆。小的修正。(它的工作原理:)):

public class MyListDemoActivity extends ListActivity { 
/** 
* Called when the activity is first created. 
*/ 
TextView tv; 
// final String[] values=new String[]{"android","Windows", 
// "ios","BlackBerry","Java",".Net","WebOS", "Ubuntu", "Windows7"}; 
//String[] content; 
List<String> content; 

AutoCompleteTextView actv; 
List<String> arr_sort = new ArrayList<String>(); 
ArrayAdapter<String> adapter; 
ListView lv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    String line = " "; 
    LineNumberReader linenoreader = null; 
    StringTokenizer stringtokanixer = null; 
    content = new ArrayList<String>(); 
    lv = (ListView) findViewById(android.R.id.list); 

    try { 

     InputStream istream = getResources().openRawResource(R.raw.hihihi); 
     InputStreamReader streamreader = new InputStreamReader(istream); 

     linenoreader = new LineNumberReader(streamreader); 
     linenoreader.mark(15); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    }// try catch ends here 


    Log.v("getting", "working"); 

    for (int i = 0; i < 6; i++) { 
     try { 
      line = linenoreader.readLine(); 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
     Log.v("getting", line); 
     stringtokanixer = new StringTokenizer(line); 

     String st = stringtokanixer.nextToken(); 
     content.add(st); 
    }// for ends here 
    arr_sort.addAll(content); 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_sort); 
    //   setListAdapter(adapter); 

    lv.setAdapter(adapter); 
    tv = (TextView) findViewById(R.id.textView1); 


    actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); 
    actv.addTextChangedListener(new TextWatcher() { 
     int len = 0; 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
             int after) { 
      // TODO Auto-generated method stub 
      arr_sort.clear(); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

      len = actv.getText().toString().trim().length(); 

      for (int i = 0; i < content.size(); i++) { 
       if (len <= content.get(i).length()) { 
        if (actv.getText().toString().trim().equalsIgnoreCase((String) content.get(i).subSequence(0, len))) { 

         arr_sort.add(content.get(i)); 

        } 


       } 

      } 

      adapter.notifyDataSetChanged(); 
     } 
    }); // text watcher class ends here 


}// on create ends here 

public void onListItemClick(ListView ls, View v, int position, long id) { 
    tv.setText(content.get(position)); 

    //tv.setText(content[position]) // in case of string 

}// endsd here onListItemClick(

}

+1

沒有它的不工作..在listview中的輸出沒有改變。 – 2012-02-29 07:07:10

+0

奇怪的是,我在設備上運行它(2.3.3) - 它工作。 – 2012-02-29 09:07:01