17

データベースからいくつかの名前を表示するオートコンプリート テキストビューを使用しています。オートコンプリート テキストビューから選択したテキストビューに名前を表示したいです。コードは次のとおりです。

ArrayList<String> s1 = new ArrayList<String>();

      for (StudentInfo cn : studentInfo) {
            s1.add(cn.getName());
        }
        ArrayAdapter<String> adapter =  new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,s1); 
       a1.setThreshold(1); 
        a1.setAdapter(adapter);

a1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
    }
        });
4

7 に答える 7

28

このようにしてみてください:

AutoCompleteTextView a1 = (AutoCompleteTextView) findViewById(...);

StudentInfo[] s1 = studentInfo.toArray(new StudentInfo[studentInfo.size()]);

ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<StudentInfo>(this, android.R.layout.simple_dropdown_item_1line, s1);
a1.setAdapter(adapter);
a1.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View arg1, int position, long arg3) {
        Object item = parent.getItemAtPosition(position);
        if (item instanceof StudentInfo){
        StudentInfo student=(StudentInfo) item;
            doSomethingWith(student);
        }
    }
});

ArrayAdapter は StudentInfo の toString() メソッドを使用して表示されるテキストを生成するため、適切な toString メソッドを実装する必要があります。

このように、この種の実装は、あらゆるオブジェクト タイプに適合させることができます。

ところで:私はandroid.R.layout.simple_dropdown_item_1lineの代わりにandroid.R.layout.simple_spinner_dropdown_itemを好む

于 2015-10-01T13:30:04.620 に答える
11

シュテファン・リヒターに感謝!List<T>アダプターを構築するときに直接使用できることを追加したいと思います。

AutoCompleteTextView autoCompleteTextView = dialogView.findViewById(R.id.autoComplete);
            // Where mStudentsInfo is List<StudentInfo>
            ArrayAdapter<StudentInfo> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, mStudentsInfo);
            autoCompleteTextView.setAdapter(adapter);
            autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Object item = parent.getItemAtPosition(position);
                    if (item instanceof StudentInfo) {
                        StudentInfo studentInfo = (StudentInfo) item;
                        // do something with the studentInfo object
                    }
                }
            });

toString()また、次のメソッドをオーバーライドすることも忘れないでStudentInfo.classください。

public class StudentInfo {

....

    @Override
    public String toString() {
        return studentName;
    }
}
于 2017-12-21T11:27:49.997 に答える
3

ビュー オブジェクトarg1から文字列の値を取得します。AutoCompleteTextView に提供された ArrayList から、この文字列を使用して項目の位置を取得します。

あなたの場合、コードは以下のようになります。

int selectedPos = s1.indexOf((String) ((TextView) arg1).getText());

selectedPos指定された ArrayList 内の文字列の位置です。

于 2014-04-29T09:00:46.030 に答える
1

ユーザー定義のデータ型を使用し、関連リストの値を設定するオートコンプリート選択から選択したアイテムを取得するには、次のコードが私のために働いた

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long arg3) {
    int selectedPos = getYourList().indexOf((((TextView)view).getText()).toString());
    SomeDAO dao = getSomeDaoList().get(selectedPos);
    //do your code
}

注: onItemClick のデフォルトのパラメーター名を arg0-parent、arg1-view、arg2-position として変更しました & SomeDAO はユーザー定義のデータ型です

于 2014-06-23T11:06:28.273 に答える