Android 来自SQLite游标的自定义ListView
我的列表包含具有名称和状态的元素。名称为Android 来自SQLite游标的自定义ListView,android,sqlite,listview,spinner,android-adapter,Android,Sqlite,Listview,Spinner,Android Adapter,我的列表包含具有名称和状态的元素。名称为字符串,状态为int。 我想要一个列表视图,其中每个元素都显示名称和用于选择状态的下拉列表。此外,背景颜色将根据状态int而改变 我有光标: String[] ItemsData= { "id", "name", "status" }; Cursor ItemsCursor = ItemsDatabase.query( Database.ITEMS_TABLE, ItemsData, null, null, null, null, null )
字符串
,状态为int
。
我想要一个列表视图
,其中每个元素都显示名称和用于选择状态的下拉列表。此外,背景颜色将根据状态int
而改变
我有光标:
String[] ItemsData= { "id", "name", "status" };
Cursor ItemsCursor = ItemsDatabase.query(
Database.ITEMS_TABLE, ItemsData, null, null, null, null, null
);
ItemsCursor.moveToFirst();
我已经创建了列表(我必须以编程方式创建):
现在,我知道我可以使用和Adapter
(比如SimpleCursorAdapter
),但我不知道怎么做,因为名称是文本视图中的字符串
,状态是微调器
,预定义值保存为数据库中的int
,默认值为状态对应的值。此外,状态也会更改背景颜色
也许我不能使用适配器。此外,如果可能的话,我不知道如何在每个列表元素中包含一个微调器。以下是我的操作方法:
listV = (ListView) findViewById(R.id.viewTest);
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
int i = 1;
String value = null;
HashMap<String, String> map;
while ((value = database.getStuff(i)) != null) {
map = new HashMap<String, String>();
map.put("title", value);
map.put("desc", "RANDOM TEXT");
map.put("img", String.valueOf(R.drawable.images));
listItem.add(map);
i++;
}
SimpleAdapter s = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.list_content,
new String[] {"img", "title", "desc"}, new int[] {R.id.img, R.id.stuff, R.id.stuff});
listV.setAdapter(s);
“R.layout.list_content”是一个.xml文件,用于设置列表中每个项目的形状,您可以添加图像、文本视图和其他所有您想要的内容
如果您想了解更多关于.xml内容的信息,请告诉我。您可以使用游标适配器根据您想要的任何布局生成行
public class CustomAdapter extends CursorAdapter {
public CustomAdapter(Context context, Cursor c) {
super(context, c, false);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Populate view with the cursor, add listeners...
}
}
您应该在xaml中为ListView创建项。本教程可能会对您有所帮助。我可以预见为列表视图的每一行设置一个下拉列表的复杂性。我更倾向于建议一个对话框,在点击单个列表元素时显示下拉列表。对,我将放置一个按钮,该按钮必须包含与每个状态号对应的文本。但是,如何使光标
将数字转换为字符串
,然后再将其绑定到列表视图
?
public String getStuff(int id) {
Cursor c = db.rawQuery("SELECT value FROM test WHERE _id = " +id, null);
return cursorToString(c);
}
private String cursorToString(Cursor c) {
if (c.getCount() == 0)
return null;
c.moveToFirst();
String s = c.getString(0);
c.close();
return s;
}
public class CustomAdapter extends CursorAdapter {
public CustomAdapter(Context context, Cursor c) {
super(context, c, false);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.row, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Populate view with the cursor, add listeners...
}
}