[英]Android: Custom CursorAdapter to alternate resources
这个问题已经困扰了我一段时间了。
我需要做的是:显示一个带有交替资源的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 []来自=新的String [] {DialogoTable.TABLE_DIALOGO +“。 + columna};
//我们将最终int []映射到的UI上的字段= 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,但是我不想创建类消息,因为我从自己的数据中获得了数据D B。
谢谢您的帮助 :)
在类似的情况下,我能够基于游标位置使用自定义的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));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.