2016-05-18 11 views
1

私はオートコンプリートのテキストビューを実装しました。私はオートコンプリートのテキストビューをクリックするたびにその提案が表示されるように設定しました。私はlist.Hereから何かを選択している私のコードです:私は{ "A"、 "abvc"、 "ajedghed"、 "B"、 "bdvhd" のような文字列配列を持っている場合」、例えばオートコンプリートのテキストビュー項目の提案を更新する

arrayAdapter = new ArrayAdapter<>(
      HomeActivity.this, android.R.layout.simple_dropdown_item_1line, array); 

    textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTV); 
    textView.setAdapter(arrayAdapter); 
    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(final View arg0) { 

      textView.showDropDown(); 

     } 
    }); 
    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      arrayAdapter.notifyDataSetChanged(); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

"bwgdydg"、 "c"、 "cswjwwd"} 私はbを入力すると "b"、 "bdvhd"、 "bwgdydg"のような "b"の関連提案がすべて表示されます。これはうまくいきますが、再度オートコンプリートのテキストビューをクリックすると、前の結果のドロップダウンが表示されますすべてのbの上に。

項目のクリックでnotifyDataSetChanged()を追加しようとしましたが、運がありません。

+0

Thsiは、オートコンプリートの設定方法である:彼はこのようにあなたのコード内のreplaceTextを追加作成し

<com.example.tester.AutoComplete android:id="@+id/autocomplete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:completionThreshold="1" /> 

ステップ3に1を使用するXML inyour autoCompleteTextView。 –

+0

あなたはそれほど多くのカスタマイズを求めているわけではありません –

+0

あなたの期待される動作は何ですか? – x0r

答えて

1

私はあなたがドロップダウンからテキストをクリックすると、あなたはオートコンプリートの提案は、あなたが選択した新しいテキストに基づいて変更したいことを理解しています。それを実現するには、autoCompleteTextViewクラスを拡張する必要があり、その後、のreplaceTextを(上書き)、あなたのonItemSelectedに、このようなtextView.replaceText((String)adapterView.getItemAtPosition(i));

何かを呼び出す:ステップ1

を:AutoCompleteTextView

public class AutoComplete extends AutoCompleteTextView { 

public AutoComplete(Context context) { 
    super(context); 
} 

public AutoComplete(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public AutoComplete(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
} 

public AutoComplete(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
} 

@Override 
protected void replaceText(CharSequence text) { 
    super.replaceText(text); 
} 
を上書き

}

ステップ2:変更t

final String [] array={"a", "abvc", "ajedghed", "b", "bdvhd", "bwgdydg", "c", "cswjwwd", 
      "ssehdk","chakra"}; 
    final AutoComplete textView=(AutoComplete) findViewById(R.id.autocomplete); 
    textView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      textView.showDropDown(); 
     } 
    }); 
    if (textView!=null) 
    textView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,array)); 

    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 

      textView.replaceText((String)adapterView.getItemAtPosition(i)); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> adapterView) { 

     } 
    }); 
+0

あなたは私を正しくしてくれました!素敵な説明がありがとうございます。このようなものはうまくいくでしょうが、オートコンプリートビューのテキストをOnclicklistenerの選択したテキストに設定してみました。 'textView.setText(((TextView)ビュー).getText());' –

+1

あなたのものははるかに短い:)。私は、選択されたテキストがtextViewに表示され、setTextがすでに呼び出されていて、提案が更新されていないので、setText()が機能しなかったことを意味していました。 – NezSpencer

+0

正確には、setTextは必要であっても呼び出されません。 –

0

試してみてください。ユーザーがもう一度クリックすると、テキストビューにテキストが含まれず、アダプターがリスト内のすべての項目を返すようにします。

if(textView.getText().toString() != null && textView.getText().toString().trim() != "") { 
     textView.setText(null); 
    } 
+0

これは予想される提案のセットを返しません。私は提案を選択されたテキストに基づいています。この場合、空のビューに対応する提案が表示されます。 –

関連する問題