[英]Change textview color in listview from button that is not in the ListView
如何從不在ListView中的按鈕更改ListView中的文本顏色? 我有一個按鈕
tvbutton.setOnClickListener(new View.OnClickListener() {
ListView tv = (ListView)findViewById(R.id.tvlist);
TextView n = (TextView)[MY ISSUE IS RIGHT HERE, I NEED THE VIEW IN THE LISTVIEW AND I HAVE NO IDEA HOW TO GET IT].findViewById(R.id.x);
n.setTextColor(WHATEVERCOLOR);
}});
我在這里從sqlite提取數據SQLiteDatabase sqLiteDatabase = controller.getReadableDatabase();
String query = "SELECT DISTINCT * FROM blah";
final Cursor cursor = sqLiteDatabase.rawQuery(query, null);
tv.setAdapter(new TVAdapter(context, cursor, 0));
這是我的TVAdapter
private static final class TVAdapter extends CursorAdapter {
MyTVAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
mInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mInflater
.inflate(R.layout.tvview, parent, false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView iv = (ImageView) view.findViewById(R.id.x1);
TextView n = (TextView) view.findViewById(R.id.x);
n.setText(cursor.getString(cursor.getColumnIndex("blahsds")));
loader.init(ImageLoaderConfiguration.createDefault(context));
loader.displayImage(cursor.getString(cursor.getColumnIndex("blahsblahs")),iv, op);
}
LayoutInflater mInflater;
}
您應該調用適配器而不是列表視圖
private static final class TVAdapter extends CursorAdapter {
private int color = 0xFF000000; // default
LayoutInflater mInflater;
MyTVAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
mInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mInflater
.inflate(R.layout.tvview, parent, false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ImageView iv = (ImageView) view.findViewById(R.id.x1);
TextView n = (TextView) view.findViewById(R.id.x);
n.setText(cursor.getString(cursor.getColumnIndex("blahsds")));
n.setTextColor(color);
loader.init(ImageLoaderConfiguration.createDefault(context));
loader.displayImage(cursor.getString(cursor.getColumnIndex("blahsblahs")),iv, op);
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
notifyDataSetChanged();
}
}
只需調用adapter.setColor(0xFFFF0000);
例如
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.