自动完成文本视图的选定项目显示为简单的Textview?

时间:2013-04-05 11:55:21

标签: java android android-edittext onitemclicklistener

我正在使用自动填充文本视图,它显示了数据库中的一些名称。我想在textview中显示一个名称,我从自动填充textview中选择了一个名称。这是我的代码:

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) {
    }
        });

7 个答案:

答案 0 :(得分:12)

以这种方式尝试:

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_spinner_dropdown_item 而不是 android.R.layout.simple_dropdown_item_1line

答案 1 :(得分:4)

感谢Stefan Richter!我想补充说,在构造适配器时可以直接使用SKView *skView = [[SKView alloc initWithFrame : self.view.frame];

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

答案 2 :(得分:3)

您的s1包含database

中的所有名称
a1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
            Log.d("your selected item",""+s1.get(position));
            //s1.get(position) is name selected from autocompletetextview
            // now you can show the value on textview. 
    }
        });

希望这会对你有所帮助,

答案 3 :(得分:3)

从视图对象 arg1 获取String的值。从提供给AutoCompleteTextView的ArrayList中,使用此String获取项目的位置。

在您的情况下,代码看起来如下所示。

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

selectedPos是提供的ArrayList中字符串的位置。

答案 4 :(得分:2)

覆盖模型类的Patient.joins(:appointments => [:physician,:hospital]).where(patients:{<condition_hash_for_patient>},physicians:{<condition_hash_for_physician>},hospitals:{<condition_hash_for_hospital>}) 方法(本例中为StudenInfo)不是一个好主意!
如果您只想获取所选项目的文本,请使用以下代码:

toString

答案 5 :(得分: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&amp; SomeDAO是用户定义的数据类型

答案 6 :(得分:0)

使用父位置为'0'

  txt_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View arg1, int position, long id) {
            Object item = parent.getItemAtPosition(0);
            if (item instanceof MYData{
                MYData data=(MYData) item;
                String dd = data.getName();
            }
        }
    });