0

私はthis検索可能なスピナー用のサードパーティライブラリを使用しています。すべてのことはいいですが、問題は、アダプタをスピンナに設定すると、ドロップダウンで文字列の代わりにオブジェクト参照が取得されることです。レトロフィットライブラリを使用したWebサービスによって配列リストが埋められています。サードパーティのAPIを使用してアダプタをスピナーに設定します。

は、以下の私の活動です:

countriesCustomAdapterInr = new CountriesCustomAdapterInr(getActivity(), R.layout.custom_spinner_items, arrayList,res); 
spinner.setAdapter(countriesCustomAdapterInr); 

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
          @Override 
          public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
           Toast.makeText(getActivity(), ""+arrayList.get(i).getFull_name()+i, Toast.LENGTH_LONG).show(); 
          } 

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

          } 
         }); 

これは私のモデルクラスです:

public class CurrencyConverter { 

    @SerializedName("short_name") 
    private String short_name; 

    @SerializedName("full_name") 
    private String full_name; 

    @SerializedName("flag") 
    private String flag; 

    public CurrencyConverter(String short_name, String full_name, String flag) { 
     this.short_name = short_name; 
     this.full_name = full_name; 
     this.flag = flag; 
    } 

    public CurrencyConverter() {} 

    public String getShort_name() { 
     return short_name; 
    } 

    public void setShort_name(String short_name) { 
     this.short_name = short_name; 
    } 

    public String getFull_name() { 
     return full_name; 
    } 

    public void setFull_name(String full_name) { 
     this.full_name = full_name; 
    } 

    public String getFlag() { 
     return flag; 
    } 

    public void setFlag(String flag) { 
     this.flag = flag; 
    } 

} 

マイアダプタ:より明確にするためにも

public class CountriesCustomAdapterInr extends ArrayAdapter<String> { 

    private ArrayList data; 
    public Resources res; 
    private LayoutInflater inflater; 

    public CountriesCustomAdapterInr(
      FragmentActivity activitySpinner, 
      int textViewResourceId, 
      ArrayList objects, 
      Resources resLocal) 
    { 
     super(activitySpinner, textViewResourceId, objects); 

     data  = objects; 
     res  = resLocal; 
     inflater = (LayoutInflater) activitySpinner.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, @NonNull ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    private View getCustomView(int position, View convertView, ViewGroup parent) { 

     View row = inflater.inflate(R.layout.custom_spinner_items, parent, false); 

     CurrencyConverter tempValues = null; 
     tempValues = (CurrencyConverter) data.get(position); 

     TextView label  = (TextView)row.findViewById(R.id.textView_short_name); 
     TextView sub   = (TextView)row.findViewById(R.id.textView_full_name); 
     ImageView flag = (ImageView)row.findViewById(R.id.imageView_flag); 
     TextView line = (TextView)row.findViewById(R.id.row); 
     if(position==0){ 
      label.setText("INR"); 
      sub.setText("India"); 
      flag.setImageResource(R.drawable.ind); 
      line.setVisibility(View.GONE); 
     } 
     else 
     { 
      // Set values for spinner each row 
      label.setText(tempValues.getShort_name()); 
      sub.setText(tempValues.getFull_name()); 
      Picasso.with(getContext()).load("http://currencyconvertor.ccube9projects.com/uploads/country_flag/"+ tempValues.getFlag()).into(flag); 
     } 


     return row; 
    } 

私が添付した画像。誰でも助けることができますか?私は2日を無駄にしましたが、私は何の答えも得ていませんでした。助けてください。

enter image description here

答えて

0

簡単な答えは、あなたがこのライブラリを使用して独自のカスタムアダプタを設定することはできませんです。 SearchableListDialog(https://github.com/miteshpithadiya/SearchableSpinner/blob/master/searchablespinnerlibrary/src/main/java/com/toptoche/searchablespinnerlibrary/SearchableListDialog.java#L160)の160行目を見ると、常にアダプターをString用の単純な配列アダプターに設定しています。

あなたが望むものを実際に実装する方法を望むなら、彼のライブラリをあなたのコードにコピーして、必要なものを修正することができます。

listAdapter = new CountriesCustomAdapterInr(getContext(), R.layout.custom_spinner_items, items, getResources()); 

あなたがこれに同様にあなたのアダプタのコンストラクタを変更する必要があります:

public CountriesCustomAdapterInr(
    Context context, 
    int textViewResourceId, 
    List<CurrencyConverter> objects, 
    Resources resLocal) { 
    super(context, textViewResourceId, objects); 

    data = objects; 
    res = resLocal; 
    inflater = (LayoutInflater) context.getSystemService(Context 
     .LAYOUT_INFLATER_SERVICE); 
} 

そして最後に、あなたは内のtoStringをオーバーライドする必要があります。例えば、SearchableListDialogにライン160を変更することは、このことにしますあなたのCurrencyConverterは、検索を動作させるために:

@Override 
public String toString() 
{ 
    return short_name; 
} 
+0

実際に私はtoString()を使うことができないので、カスタムアイテムリストに2つのテキストと1つのイメージがあります。テキストが1つしかない場合は、toString()を使用できますが、使用できないデザインをカスタマイズできます。他の解決策? –

+1

toString()は、検索フィルターを機能させるためのものです。ライブラリのSearchableListDialogを変更すると、必要なカスタムアダプタが提供されます。 –

+0

私はあなたが何を求めているのか分かりません。上記のコードは動作し、必要なものを生成します。 –

関連する問題