2017-05-16 9 views
0

現在、フィルタリングされた配列リストをオートコンプリートテキストビュー用のアダプタに設定するために、要素の位置を取得する必要がありますか?ArrayAdapterで2つの値を渡す方法

以下はコードです。 タイトルを取得するために繰り返しましたが、そのためにtabList.fromにあるURIが必要です。これをArrayAdapter.Soに渡す必要があります。どのようにして自分の要件に進むことができますか?

if (tabsList != null) { 
      Iterator iterator = tabsList.iterator(); 
      while (iterator.hasNext()) { 
       Tabs tabs = (Tabs) iterator.next(); 
       Iterator iterator1 = tabs.getCardInfo().iterator(); 
       while (iterator1.hasNext()) { 

        Cards card = (Cards) iterator1.next(); 
        titleInCard.add(card.getTitle()); 

       } 
      } 
     } 





    @Override 
     public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
      charSequence = charSequence.toString().toUpperCase(); 
      final List<String> filteredList = new ArrayList<String>(); 
      for (int k = 0; k < titleInCard.size(); k++) 
      { 
       String titleData = titleInCard.get(k).toUpperCase(); 
       if (titleData.contains(charSequence)) { 
        filteredList.add(titleData); 
        Log.i("TAG", "onTextChanged: " + filteredList.size()); 
        ArrayAdapter adapter = new ArrayAdapter(WebLoadActivity.this, android.R.layout.simple_list_item_1, filteredList); 
        mSearchEditTExt.setAdapter(adapter); 
        mSearchEditTExt.setThreshold(1); 


       } 
      } 
     } 
+0

独自の配列アダプターを作成します。 – Zoe

+0

これは、あなたが始めたいと望むなら良いガイドです:[カスタム配列アダプターの使い方](https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView#using-a-custom- arrayadapter) – jayeshsolanki93

答えて

0

カスタムArrayAdapterが必要です。クラスを作成し、2つの値を渡します。

 public class SomeList { 
      private String mFirstValue; 
      private String mSecondValue; 


      public SomeList(String firstvalue, String secondvalue) { 
       mFirstValue = firstvalue; 
       mSecondValue = secondvalue; 
      }  

      public String getFirstValue() { 
       return mFirstValue; 
      } 

      public String getSecondValue() { 
       return mSecondValue; 
      } 
     } 

最後に

class SomeCustomAdapter extends ArrayAdapter<SomeList> { 

    SomeCustomAdapter(Context context, ArrayList<SomeList> filteredList) { 
     super(context, 0, filteredList); 

    } 

    @NonNull 
    @Override 
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
     //initialize view 
     View listItemView = convertView; 
     if (listItemView == null) { 
      listItemView = LayoutInflater.from(getContext()).inflate(
        R.layout.custom_list_item, parent, false); 
     } 
     //get current object from SomeList class 
     SomeList currentSomeList = getItem(position); 

     //Placeholder for first value 
     TextView mFirstTextView = (TextView) listItemView.findViewById(R.id.text_view_one); 
     mFirstTextView.setText(String.valueOf(currentSomeList.getFirstValue())); 

     //Placeholder for second value 
     TextView mSecondTextView = (TextView) listItemView.findViewById(R.id.text_view_two); 
     mSecondTextView.setText(currentSomeList.getSecondValue()); 

     return listItemView; 
    } 
} 

カスタムアダプタを作成し、2 textviewsでcustom_list_itemを作成することを忘れないでください、あなたのクラスのオブジェクトのArrayListを作成し、オブジェクト

final ArrayList<Word> filteredList = new ArrayList<>(); 

    // fill it up with objects eg: 
    earthquakes.add(new SomeList("George Washington", "April 30, 1789 – March 4")); 

    SomeCustomAdapter adapter = new SomeCustomAdapter(this, filteredList); 
    mSearchEditTExt.setAdapter(adapter); 

でそれを埋めます。例:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_width="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/text_view_one" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/text_view_two" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
関連する問題