2013-04-05 26 views
8

我使用的是自動完成的TextView其中顯示了從database.I一些名字想展示中,我從自動完成textview.Here選擇一個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) { 
    } 
     }); 
+0

什麼是A1在代碼,你想在textview或edittext中拍攝選定的項目? – 2013-04-05 12:00:15

+0

a1是自動完成的textview,我想在textview中顯示選定的項目。 – bhoot4242 2013-04-05 12:12:25

+0

在自動完成文本視圖中,當您選擇自動完成文本視圖中自動設置的項目時。沒有必要爲此提供額外的方法。請點擊此處:http://dj-android.blogspot.in/2013/04/android-autocompletetextview-inside.html – 2013-04-05 12:36:53

回答

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

希望這將幫助你,所有的名字

+6

注意!在此示例中,位置參數不是s1中的位置,它是索引,無論在選擇時自動完成發生的情況如何。 – 2015-04-30 11:41:00

3

從查看對象arg1獲取String的值。從提供給AutoCompleteTextView的ArrayList中獲取使用此String的項目的位置。

在你的情況下,代碼將如下所示。

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

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

1

若要選擇從使用用戶定義的數據類型,並設置相關list.following碼值工作對我來說

@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作爲父母, ARG1視圖,ARG2位置& SomeDAO是用戶定義的數據類型

7

嘗試這種方式:

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

的ArrayAdapte r使用StudentInfo的toString()方法生成顯示的文本,因此您需要實現一個很好的toString方法。

這樣,這種實現可以適應任何對象類型。

順便說一句:我喜歡android.R.layout.simple_spinner_dropdown_item,而不是android.R.layout.simple_dropdown_item_1line

+0

它沒有顯示下拉,你能幫我嗎? – Shivang 2016-05-17 08:59:44

+1

parent.getItemAtPosition(position);其顯示的列表給出位置,所以位置與原始數據不匹配,所以它沒有給出所需的輸出 – Shivang 2016-05-20 09:26:12

1

覆蓋的模型類toString方法(StudenInfo在這種情況下)是不是一個好主意!
如果你只是想獲得所選項目的文本,使用此代碼:

autoCompleteView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       String selectedItem = (String) parent.getItemAtPosition(position); 
       // here is your selected item 
      } 
     }); 
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(); 
      } 
     } 
    }); 
1

感恩斯特凡裏希特!我想補充一點,可以使用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 
        } 
       } 
      }); 

而且也不要忘了覆蓋StudentInfo.classtoString()方法:

public class StudentInfo { 

.... 

    @Override 
    public String toString() { 
     return studentName; 
    } 
} 
相關問題