2010-11-17 22 views
5

J'essaie de remplacer MultiAutoCompleteTextView liste déroulante avec un ListView, dont il devrait avoir la même fonctionnalité que la liste déroulante, ce qui signifie, lorsque je clique dans un des éléments, il doit être ajouté à MultiAutoCompleteTextView, etc., mais en filtrant ListView au fur et à mesure de la frappe.Comment remplacer MultiAutoCompleteTextView liste déroulante

Alors je suis venu avec ce code brut sans succès:

filterable_listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:orientation="vertical"> 

<MultiAutoCompleteTextView 
    android:layout_height="wrap_content" android:layout_width="fill_parent" 
    android:hint="@string/To" android:id="@+id/search_box"></MultiAutoCompleteTextView> 

<ListView android:id="@android:id/list" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_weight="1"/> 

</LinearLayout> 

AutoCompleteActivity.java

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.filterable_listview); 
manager = new ContactManager(); 
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, manager.getContacts()); 

searchEdit = (MultiAutoCompleteTextView) findViewById(R.id.search_box); 

searchEdit.addTextChangedListener(filterTextWatcher); 

searchEdit.setTokenizer(new SpaceTokenizer()); 

setListAdapter(adapter); 

getListView().setOnItemClickListener(
    new ListView.OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
        //Here is one issues 
    searchEdit.append(adapter.getItem(position)); 
    } 
}); 
} 

private TextWatcher filterTextWatcher = 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) { 
      adapter.getFilter().filter(s); 
     } 

}; 

Je dois principaux problèmes:

1) Lorsque je clique sur un élément, le texte entier est ajouté, Je sais que le problème repose sur:

searchEdit.append (adapter.getItem (position)); Mais, comment puis-je faire du texte couvrant comme autocomplete régulière fait?

2) Une fois un élément a été sélectionné, l'entrée suivante ne montre pas la suggestion plus (malgré SpaceTonekizer)

J'espère que mon explication était claire.

Merci à l'avance

Répondre

6

Si quelqu'un a besoin de faire quelque chose comme je l'ai besoin, je fait ce

private MultiAutoCompleteTextView searchEdit; 

private ArrayAdapter<String> adapter = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_dropdown_item_1line, manager 
        .getContacts()); 

    setListAdapter(adapter); 

    searchEdit = (MultiAutoCompleteTextView) findViewById(R.id.search_box); 

    searchEdit.addTextChangedListener(filterTextWatcher); 

    searchEdit.setTokenizer(new SpaceTokenizer()); 

    searchEdit.setAdapter(adapter); 

    searchEdit.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      String t = ((TextView) v).getText().toString(); 
      String f = searchEdit.getText().toString(); 

      int s = searchEdit.getSelectionStart(); 
      int i = s; 

      while (i > 0 && f.charAt(i - 1) != ' ') { 
       i--; 
      } 

      adapter.getFilter().filter(t.substring(i, s)); 
     } 
    }); 

    okButton = (Button) findViewById(R.id.ok); 
    okButton.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Bundle stats = new Bundle(); 
      stats.putString("ConversationName", searchEdit.getText() 
        .toString()); 

      Intent i = new Intent(); 
      i.putExtras(stats); 
      setResult(RESULT_OK, i); 
      finish(); 
     } 
    }); 

    searchEdit.setOnKeyListener(new OnKeyListener() { 

     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT 
        || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT 
        || keyCode == KeyEvent.KEYCODE_DPAD_UP 
        || keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { 

       String t = ((TextView) v).getText().toString(); 
       String f = searchEdit.getText().toString(); 

       int s = searchEdit.getSelectionStart(); 
       int i = s; 

       while (i > 0 && f.charAt(i - 1) != ' ') { 
        i--; 
       } 

       adapter.getFilter().filter(t.substring(i, s)); 

       return false; 
      } 

      return false; 
     } 
    }); 

    getListView().setOnItemClickListener(
      new ListView.OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

        String t = adapter.getItem(position); 
        String f = searchEdit.getText().toString(); 

        int s = searchEdit.getSelectionStart(); 
        int i = s; 

        while (i > 0 && f.charAt(i - 1) != ' ') { 
         i--; 
        } 

        searchEdit.getText().insert(s, t.substring(s - i)); 
       } 
      }); 
} 


    @Override 
protected void onDestroy() { 
    super.onDestroy(); 
    searchEdit.removeTextChangedListener(filterTextWatcher); 
} 


private TextWatcher filterTextWatcher = new TextWatcher() { 

    public void afterTextChanged(Editable s) { 
     okButton.setEnabled(searchEdit.getText().toString().trim() 
       .length() > 0); 
    } 

    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    public void onTextChanged(CharSequence s, int start, int before, 
      int count) { 
     adapter.getFilter().filter(s); 
    } 

}; 



public class SpaceTokenizer implements Tokenizer { 

public int findTokenStart(CharSequence text, int cursor) { 
    int i = cursor; 

    while (i > 0 && text.charAt(i - 1) != ' ') { 
     i--; 
    } 
    while (i < cursor && text.charAt(i) == ' ') { 
     i++; 
    } 

    return i; 
} 

public int findTokenEnd(CharSequence text, int cursor) { 
    int i = cursor; 
    int len = text.length(); 

    while (i < len) { 
     if (text.charAt(i) == ' ') { 
      return i; 
     } else { 
      i++; 
     } 
    } 

    return len; 
} 

public CharSequence terminateToken(CharSequence text) { 
    int i = text.length(); 

    while (i > 0 && text.charAt(i - 1) == ' ') { 
     i--; 
    } 

    if (i > 0 && text.charAt(i - 1) == ' ') { 
     return text; 
    } else { 
     if (text instanceof Spanned) { 
      SpannableString sp = new SpannableString(text + " "); 
      TextUtils.copySpansFrom((Spanned) text, 0, text.length(), 
        Object.class, sp, 0); 
      return sp; 
     } else { 
      return text + " "; 
     } 
    } 
} 
} 

Le xml ressemble à ceci:

<?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" 
android:drawingCacheQuality="high"> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="horizontal"> 


    <MultiAutoCompleteTextView 
     android:layout_marginLeft="1dip" 
     android:layout_marginTop="1dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_width="fill_parent" 
     android:dropDownHeight="0px" 
     android:hint="@string/To" 
     android:id="@+id/search_box" 
     ></MultiAutoCompleteTextView> 



    <Button android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Ok" 
     android:id="@+id/ok" 
     android:enabled="false" 
     /> 


</LinearLayout>  
<ListView android:id="@android:id/list" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_weight="1" /> 

</LinearLayout> 

Hope this aide

+0

pouvez-vous me dire le okButton, où vous l'avez mis? – pengwang

+1

Désolé, je n'ai pas mis à jour xml, vérifiez xml ajouté. – vsm

+0

je développe ListActivity, et à la place manager.getContacts() j'ai utilisé une chaîne [], mais je reçois 12-07 14: 29: 56.459: ERROR/AndroidRuntime (423): java.lang.RuntimeException: Impossible de démarrer l'activité ComponentInfo { com.test.testMultiAutoCompleteTextView/com.test.testMultiAutoCompleteTextView.testMultiAutoCompleteTextView}: java.lang.NullPointerException – pengwang