2016-10-31 11 views
0

私の目的は、動作するスピナー(カーソルアダプターを介して入力)を交互の背景に変換することです。同様に: -カーソルアダプタを使用してカスタムレイアウトでスピナーを使用するにはどうすればよいですか?

enter image description here

すべてが正常に動作どこ現在、私は、これを持っている: -

enter image description here

これは、カーソルの特殊なアダプタ(すなわち、プレーンドロップダウン付き)内の関連動作するコードです: -

@Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 

     return LayoutInflater.from(context).inflate(R.layout.activity_aisle_shop_list_selector, parent, false); 
    } 
    @Override 
    public void bindView(View view,Context context, Cursor cursor) { 
     determineViewBeingProcessed(view,"BindV",-1); 

     TextView shopname = (TextView) view.findViewById(R.id.aaslstv01); 
     shopname.setText(cursor.getString(shops_shopname_offset)); 

    } 

私はのオーバーライドを追加しようとしました(以下のコード)。私は、行の色を交互にしたいのですが、ドロップダウン・ビューは空白です。しかし、セレクタの外側をクリックすると、データが取り込まれます(したがって、上記のように、私が望むもののスクリーンショットをどのように管理するか)。 Selectionが正しいItemを設定します。

Iレイアウトを膨張させた後returnを削除する場合、ドロップダウンビューが移入されているが、他の行からのデータを有する(但し、選択が正しい項目を選択)

public View getDropDownView(int position, View convertview, ViewGroup parent) { 
     View v = convertview; 
     determineViewBeingProcessed(v,"GetDDV",position); 
     if(v == null) { 
      v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_aisle_shop_list_entry, parent, false); 
      return v; 
     } 
     Context context = v.getContext(); 

     TextView shopname = (TextView) v.findViewById(R.id.aasletv01); 

     shopname.setText(getCursor().getString(shops_shopname_offset)); 

     if(position % 2 == 0) { 
      v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewroweven)); 
     } else { 
      v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewrowodd)); 
     } 
     return v; 
    } 

答えて

0

手がかりはそれらがちょうどIました十分に難しいとは思わなかった。問題は、カーソルをgetCursor()で取得する必要があるため、カーソルが間違った位置にあることです。

また、inflate後のreturnは時期尚早です(これはコメントアウトされています)。

getCursor().moveToPosition(position);を追加すると、カーソルからデータにアクセスする前に問題が解決されます。

おそらくもっと正確には、一方の方法がもう一方のよりも正しいかどうかのコメントが高く評価されます)。

Cursor cursor = getCursor(); 
    cursor.moveToPosition(position); 

を、次いでcursorと後続getCursor()を交換(必須ではない)にも動作 - :追加します。

のでgetDropDownView方法のための最終的なコードは次のようになります -

public View getDropDownView(int position, View convertview, ViewGroup parent) { 
     View v = convertview; 
     determineViewBeingProcessed(v,"GetDDV",position); 
     if(v == null) { 
      v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_aisle_shop_list_entry, parent, false); 
      //return v; 
     } 
     Context context = v.getContext(); 
     Cursor cursor = getCursor(); 
     cursor.moveToPosition(position); 


     TextView shopname = (TextView) v.findViewById(R.id.aasletv01); 

     shopname.setText(cursor.getString(shops_shopname_offset)); 

     if(position % 2 == 0) { 
      v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewroweven)); 
     } else { 
      v.setBackgroundColor(ContextCompat.getColor(context,R.color.colorlistviewrowodd)); 
     } 
     return v; 
    } 
関連する問題