gpt4 book ai didi

java - 关于mt ListView中画行的咨询

转载 作者:行者123 更新时间:2023-11-30 09:33:35 24 4
gpt4 key购买 nike

如何在我的 ListView 中将小于 100 的每个值都绘制成红色?

我有连接到 my_list.xml

的 ListView

我这样连接光标:

public void update_list(String NN) {
c = db.rawQuery(NN, null);
startManagingCursor(c);
String[] from = new String[]{"_id","Store","Makat","Des","Qty" };
int[] to = new int[]{R.id.txtID, R.id.txtStore,R.id.txtMakat ,R.id.txtDes ,R.id.txtQty};
SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.my_list, c, from, to);
setListAdapter(notes);
}

怎么做?

最佳答案

我的建议是制作您自己的适配器来扩展 SimpleCursorAdapter。在其中,您应该覆盖创建每个行 View 的 getView 方法。

    class MyCustomAdapter extends SimpleCursorAdapter{
private Context context;
private Cursor c;

public MyCustomAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
this.c = c;
this.context = context;
}

@Override
public View getView(int pos, View inView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

this.c.moveToPosition(pos);

//get the value from cursor, evaluate it, and set the color accordingly
int columnIndexOfQty = 4;
int qty = c.getInt(columnIndexOfQty);

if(qty < 100){
rowView.setBackgroundColor(Color.RED);
}
return rowView;
}

}

可能会有一些错误,但我想你一定明白了。

关于java - 关于mt ListView中画行的咨询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12090080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com