2017-02-28 36 views
2

我正在將可搜索的微調器集成到我的應用中。下面是我的代碼可搜索的微調適配器中的問題android

gradle這個文件

compile 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1' 

xml文檔

<com.toptoche.searchablespinnerlibrary.SearchableSpinner 
    android:id="@+id/spinner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:hintText="Select Country"/> 

Man.java

public class MainActivity extends AppCompatActivity { 

SearchableSpinner mSearchableSpinner; 
ArrayList<GetCountry> mGetCountries; 
PriorityAdapter mPriorityAdapter; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mSearchableSpinner = (SearchableSpinner) findViewById(R.id.spinner); 

    mGetCountries = new ArrayList<>(); 
    GetCountry mGetCountry = new GetCountry(); 
    mGetCountry.setId("1"); 
    mGetCountry.setName("India"); 
    mGetCountries.add(mGetCountry); 

    GetCountry mGetCountry2 = new GetCountry(); 
    mGetCountry2.setId("2"); 
    mGetCountry2.setName("USA"); 
    mGetCountries.add(mGetCountry2); 


    GetCountry mGetCountry3 = new GetCountry(); 
    mGetCountry3.setId("3"); 
    mGetCountry3.setName("UK"); 
    mGetCountries.add(mGetCountry3); 

    GetCountry mGetCountry4 = new GetCountry(); 
    mGetCountry4.setId("4"); 
    mGetCountry4.setName("CHINE"); 
    mGetCountries.add(mGetCountry4); 

    GetCountry mGetCountry5 = new GetCountry(); 
    mGetCountry5.setId("5"); 
    mGetCountry5.setName("MALASIYA"); 
    mGetCountries.add(mGetCountry5); 

    mPriorityAdapter=new PriorityAdapter(mGetCountries); 
    mSearchableSpinner.setAdapter(mPriorityAdapter); 
} 


public class PriorityAdapter extends ArrayAdapter<GetCountry> { 

    ArrayList<GetCountry> list; 

    public PriorityAdapter(ArrayList<GetCountry> list) { 
     super(MainActivity.this, R.layout.spin_layout, list); 
     this.list = list; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { // Ordinary 


     return initView(position, convertView, parent); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, 
           ViewGroup parent) { // This view starts when we click the 
     // spinner. 
     return initView(position, convertView, parent); 
    } 

    public View initView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflator = getLayoutInflater(); 
     convertView = inflator.inflate(R.layout.spin_layout, null); 
     TextView mTextView = (TextView) convertView 
       .findViewById(android.R.id.text1); 
     mTextView.setText(list.get(position).getName()); 
     return convertView; 
    } 

} 

} 

當我運行上面的代碼我得到像下面的圖像輸出。自定義arraylist數據不顯示它是java的每個項目的打印垃圾值 Spinner Output

任何想法我怎麼能解決這個問題?

編輯

public class PriorityAdapter extends ArrayAdapter<GetCountry> { 

    ArrayList<GetCountry> list; 

    public PriorityAdapter(ArrayList<GetCountry> list) { 
     super(MainActivity.this, R.layout.spin_layout, list); 
     this.list = list; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { // Ordinary 


     return initView(position, convertView, parent); 
    } 

    @Nullable 
    @Override 
    public GetCountry getItem(int position) { 
     return super.getItem(position); 
    } 



    @Override 
    public View getDropDownView(int position, View convertView, 
           ViewGroup parent) { // This view starts when we click the 

     View view = convertView; 
     LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
     view = layoutInflater.inflate(R.layout.spin_layout_1, parent, false); 

     TextView tv= (TextView) view.findViewById(R.id.text1); 
     tv.setText(list.get(position).getName()); 
     return view; 

    } 

    public View initView(int position, View convertView, ViewGroup parent) { 


     View view = convertView; 
     LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
     view = layoutInflater.inflate(R.layout.spin_layout_1, parent, false); 

     TextView tv= (TextView) view.findViewById(R.id.text1); 
     tv.setText(list.get(position).getName()); 
     return view; 
    } 

} 

GetCountry.java

public class GetCountry { 
String name; 
String id; 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 
} 
+0

我遇到了同樣的問題,我唯一可以添加的是DropDownView並沒有真正考慮到,而是您的類的.toString()。 我不(知道爲什麼,但似乎SpinnerSearchable不使用正確的適配器,或者可能我們失去了一些東西。 希望有人可以提供幫助。 乾杯 – Gary89

+0

如何解決這個問題?我 –

回答

1

你可以嘗試改變getView和getDropDownView返回值

這樣;

View view = convertView; 
LayoutInflater layoutInflater = LayoutInflater.from(getContext()); 
view = layoutInflater.inflate(R.layout.spinneradapter, parent, false); 

TextView tv= (TextView) view.findViewById(R.id.tvEkipCantasiEkipman); 
tvAdet.setText(arrayListItem); 
return view; 
+0

onur:我試着用你的代碼,但仍然面臨相同的問題 –

+0

onur:檢查我的編輯部分我嘗試按照你的建議,但仍然面臨同樣的問題 –

+0

請分享你的GetCountry類,你已經定義了setId, setName和getName方法? –

0

因爲getView和其他在PriorityAdapter中聲明的方法根本沒有調用,所以你得到這個列表。 那麼爲什麼要使用自定義適配器,而不是隻是去爲下面的簡單陣列適配器 -

String[] names = new String[]{"India","CHINA","UK","US","MALYSIA"}; 
     ArrayAdapter arrayAdapter = new ArrayAdapter(SearchActivity.this,android.R.layout.simple_spinner_dropdown_item,names); 
     mSearchableSpinner.setAdapter(arrayAdapter); 

添加在活動的onCreate方法這些線路,你會得到你的正確的下拉列表。

+0

其實我有自定義列表,所以我必須設計自定義適配器。任何解決方案@ B.shruti –