SimpleCursorAdapter - 将 2 列设置为 1 个视图
SimpleCursorAdapter - set 2 columns to 1 view
我有一个 SQLite 数据库,我通过 SimpleCursorAdapter
在 ListView
中显示数据。我有 2 列,但我想在 1 TextView
中显示它们。我怎样才能做到这一点?谢谢
试试这个代码,它可能对你有帮助
从Oncreate调用Listview的方法
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
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);
}
}
我有一个 SQLite 数据库,我通过 SimpleCursorAdapter
在 ListView
中显示数据。我有 2 列,但我想在 1 TextView
中显示它们。我怎样才能做到这一点?谢谢
试试这个代码,它可能对你有帮助
从Oncreate调用Listview的方法
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
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);
}
}