SimpleCursorAdapter - 将 2 列设置为 1 个视图 [英] SimpleCursorAdapter - set 2 columns to 1 view
本文介绍了SimpleCursorAdapter - 将 2 列设置为 1 个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 SQLite 数据库,我通过 SimpleCursorAdapter
在 ListView
中显示数据.我有 2 列,但我想在 1 个 TextView
中显示它们.我怎样才能做到这一点?谢谢
I have a SQLite Datebase from which I am displaying data in ListView
by SimpleCursorAdapter
. I have 2 columns but I want to displey them in 1 TextView
. How can I do that? Thanks
推荐答案
试试这个代码,它可能对你有帮助
Try This Code , it may help you
从 Oncreate 调用 Listview 的方法
Method For calling Listview from Oncreate
private void populateListViewFromDB1()
{
Cursor cursor = helper1.getAllData(Sharedemail);
startManagingCursor(cursor);
String[] productname = new String[]
{
DataBaseHelper.KEY_NAMEVALUE,
DataBaseHelper.KEY_TYPE,
};
int[] viewproduct = new int[]
{
R.id.textView1,
};
// Create Adapter
MySimpleCursoradapter myCursorAdapter = new MySimpleCursoradapter
(this,
R.layout.listview,
cursor,
productname,
viewproduct);
ListView List = (ListView) findViewById(R.id.listView1);
List.setAdapter(myCursorAdapter);
}
}
MySimpleCursoradapter.java
MySimpleCursoradapter.java
public class MySimpleCursoradapter extends SimpleCursorAdapter {
public MySimpleCursoradapter(Context context, int layout,
Cursor cur, String[] from, int[] to) {
super(context, layout, cur, from, to);
}
@SuppressWarnings("static-access")
@Override
public View newView(Context con, Cursor c, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(con.LAYOUT_INFLATER_SERVICE);
View retView = inflater.inflate(R.layout.listview, null);
return retView;
}
public void bindView(View v, Context context, Cursor c) {
String pname = c.getString(c.getColumnIndex(DataBaseHelper.KEY__NAMEVALUE));
String issuetype = c.getString(c.getColumnIndex(DataBaseHelper.KEY_TYPE));
TextView name_text = (TextView) v.findViewById(R.id.textView1);
name_text.setText(pname +":"+ issuetype);
}
}
这篇关于SimpleCursorAdapter - 将 2 列设置为 1 个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文