1

この問題は私の頭の中でしばらく立ち往生しています。

私がする必要があること: listView 内の項目の代替リソースを含む listview を表示します。

私の問題は何ですか: これまでのところ、リソースを代替してデータを表示しないか、データを表示しても代替リソースを表示できません。最初の項目は毎回うまく機能しますが、それ以降は機能しません。私は非常に近いと思いますが、何が悪いのかわかりません...

私がしたこと: カスタムのシンプルなカーソル アダプターを使用しました。

コードはどこにありますか:

public class DialogCursor extends SimpleCursorAdapter {

private LinearLayout wrapper;
private TextView burbuja;

  public DialogCursor(Context context, int layout, Cursor c, String[] from,
        int[] to, int flags) {
    super(context, layout, c, from, to, flags);
    // TODO Auto-generated constructor stub
}



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

    View row = convertView;

    if (row == null) {


        Context context = parent.getContext();
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.dialogo_row, parent, false);

    }
    burbuja = (TextView) row.findViewById(R.id.idiomaselec);
    wrapper = (LinearLayout) row.findViewById(R.id.wrapper);

   //get reference to the row
   View view = super.getView(position, convertView, parent); 
   Log.d("Dialogo","enters getview");
   Log.d("Dialogo",Integer.toString(position));
   //check for odd or even to set alternate colors to the row background
   if(position % 2 == 0){  
    Log.d("Dialogo","Even");
    burbuja.setBackgroundResource(R.drawable.bubble_green);
    wrapper.setGravity(Gravity.LEFT);
   }
   else {
    Log.d("Dialogo","not even");
    burbuja.setBackgroundResource(R.drawable.bubble_yellow);
    wrapper.setGravity(Gravity.RIGHT);  
   }

   return row; 
  }

}

カーソル アダプターは、この他のクラスから呼び出されます (関連部分のみを表示)

String[] from = new String[] {DialogoTable.TABLE_DIALOGO + "." +列};

// final int[] to = new int[] { R.id.idiomaselec}; // UI 上のフィールド。

Log.d("Dialogo","entra en fillData2");
getLoaderManager().initLoader(0, null, this);
if (bot)  {
    Log.d("Dialogo","entra en fillData2.5");
    getLoaderManager().restartLoader(0, null, this);
}

adapter2 = new DialogCursor(this, R.layout.dialogo_row, null, from, to, 0);

setListAdapter(adapter2); 

出力: 行 (コードの最後の行) を返すと、適切な場所にバックグラウンド リソースが取得されますが、データはありません ビュー (コードの最後の行) を返すと、データは取得されますが、最初の項目のみが適切な背景を持っています資力。

最後の注意: 私はこの例に従いました http://adilsoomro.blogspot.com/2012/12/android-listview-with-speech-bubble.html しかし、私はクラスメッセージを作成したくありません。 DB。

ご協力ありがとうございました :)

4

1 に答える 1

0

同様のケースで、カーソル位置に基づいて、カスタムのcursorAdapter代替リソースを持つことができました。entryView がビューで渡される bindView に次のコードを入れます。getView をまったくオーバーライドしています。

if(cursor.getPosition()%2 == 1){
     entryView.findViewById(R.id.title_relative).setBackgroundColor(getResources().getColor(R.color.orange));
}else{
     entryView.findViewById(R.id.title_relative).setBackgroundColor(getResources().getColor(R.color.blue));
}
于 2014-02-21T23:59:11.273 に答える