【问题标题】:Android: Custom CursorAdapter to alternate resourcesAndroid:自定义 CursorAdapter 以替代资源
【发布时间】:2013-05-09 07:09:57
【问题描述】:

这个问题已经在我脑海中停留了一段时间。

我需要做的: 为 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 + "." + 列 };

// 我们映射到的 UI 上的字段 final int[] to = new int[] { R.id.idiomaselec};

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 但我不想创建类消息,因为我从我的数据库中获取数据。

感谢您的帮助:)

【问题讨论】:

    标签: android listview layout android-cursoradapter alternate


    【解决方案1】:

    在类似的情况下,我能够根据光标位置自定义 cursorAdapter 备用资源。我将以下代码放在我的 bindView 中,其中 entryView 是传入的视图。我完全覆盖了 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));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多