我有一个 ListView ,它从 SQLite 数据库填充其内容。 这是我的代码:
ListView listView = (ListView) findViewById(R.id.lstText);
listView.setOnItemClickListener(this);
listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, null, null, null),
new String[] { Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, new int[] {
R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org}));
当它显示在列表中时,我想将一个额外的字符串添加到上面的某些 TextViews
中。例如,我想在 R.id.txt_org
上添加一个带有单词“from”的 String
,在数据库 Database 中填充的字符串之前。项目.C_ORGANIZATIONTITLE
假设填充的 String
是:New Organisation,
使用额外的 String
"from"将显示的是:from New Organisation
有人可以帮我吗?非常感谢。
编辑:
仅供引用,这是我的 SimpleCursorAdapter 方法:
class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
loader = new ImageLoader(context);
this.context = context;
}
Context context=null;
ImageLoader loader = null;
public void setViewImage(ImageView v, String value) {
v.setTag(value);
loader.DisplayImage(value, context, v);
}
}
最佳答案
由于您已经在使用自定义适配器,因此覆盖适配器的 bindView()
和 newView()
方法,而不是 getView()
。这样您就不必手动处理回收行的 View 。
在这些方法中,您可以从生成的游标中获取数据并在将其绑定(bind)到您的行 View 之前对其进行操作。
//编辑:下面还有一些代码。请注意,这只是一个粗略的轮廓,绝不是完整的或经过测试的。
class MySimpleCursorAdapter extends SimpleCursorAdapter {
private ImageLoader mLoader = null;
private LayoutInflater mInflater = null;
private int mBusinessNameIndex = -1;
private int mSmallImageIndex = -1;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
mLoader = new ImageLoader(context);
mInflater = getLayoutInflater();
mBusinessNameIndex = c.getColumnIndexOrThrow(Database.Project.NAME);
mSmallImageIndex = c.getColumnIndexOrThrow(Database.Project.C_SMALLIMAGE);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.row, null);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Get your views from 'view'
TextView someTextView = (TextView) view.findViewById(R.id.xxx);
ImageView someImageView = (ImageView) view.findViewById(R.id.yyy);
// Set the data
someTextView.setText("from " + cursor.getString(mBusinessNameIndex));
mLoader.DisplayImage(cursor.getString(mSmallImageIndex ), context, someImageView);
}
}
关于Android:向 TextView 添加一个额外的字符串,该字符串从数据库填充其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8092982/