我有一个微调器,我需要在sqlite数据库中填充子名称。我有一个方法来返回一个孩子的列表。
public List<ChildUser> getChildren() {
List<ChildUser> childList = new ArrayList<ChildUser>();
//select columns
String[] tableColumns = new String[]{"id", "childname", "nickname"};
Cursor cursor = db.query("child", tableColumns, "parent =? ", new String[]{_parentName}, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
ChildUser child = new ChildUser();
//take values from database
child.setId(cursor.getInt(0));
child.setChildName(cursor.getString(1));
child.setNickName(cursor.getString(2));
//add to db
childList.add(child);
cursor.moveToNext();
}
return childList;
}
然后我从一个片段调用这个方法来填充一个微调器,但是,我得到了另一个显示的子列表属性,如何在微调器中显示我所选项的text属性? (请单击链接以查看我的微调器显示的确切内容)
final ChildDataBase cdb = new ChildDataBase(getActivity());
final List<ChildUser> childList = cdb.getChildren();
Spinner spinner = (Spinner) v.findViewById(R.id.del_child_spinner);
ArrayAdapter<ChildUser> spinnerAdapter = new ArrayAdapter<ChildUser>(getActivity(), android.R.layout.simple_spinner_item, childList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
childId = childList.get(position).getId();
Toast succ = Toast.makeText(Home.super.getContext(), childList.get(position).getChildName() + " Selected.", Toast.LENGTH_SHORT);
succ.show();
return;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast succ = Toast.makeText(Home.super.getContext(), "No child selected.", Toast.LENGTH_SHORT);
succ.show();
}
});
Spinner&lt; ---点击此处查看微调器
我也有一个自定义列表适配器,我不知道我是否可以以某种方式应用它来完成这项任务。
/**
* Created by Victor on 4/3/2016.
*/
public class ChildListAdapter extends ArrayAdapter<ChildUser> {
private FragmentTransaction fragmentTransaction;
private android.support.v4.app.FragmentManager fragmentManager;
private final Context context;
private final List<ChildUser> childList;
public ChildListAdapter(Context context, List<ChildUser> childList){
super(context, R.layout.fragment_child_list_layout, childList);//fragment_child_list
this.context = context;
this.childList= childList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.fragment_child_list_layout,parent,false);
Button childName = (Button)rowView.findViewById(R.id.childButton);
childName.setText(childList.get(position).getNickName());
childName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String selectedChild = childList.get(position).getChildName();
// do something with selected item
Toast fail = Toast.makeText(ChildListAdapter.super.getContext(), selectedChild + " selected", Toast.LENGTH_SHORT);
fail.show();
Globals._selectChild = selectedChild;
TaskList taskList = new TaskList();
fragmentTransaction = ((FragmentActivity)context).getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragholder, taskList);
fragmentTransaction.commit();
}
});
return rowView;
}
}
这是我的ChildUser对象
public class ChildUser {
String childName;
String nickName, gender, parent;
int id, age;
public void setChildName(String childName)
{
this.childName = childName;
}
public String getChildName()
{
return this.childName;
}
public void setNickName(String nickName)
{
this.nickName = nickName;
}
public String getNickName()
{
return this.nickName;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getGender()
{
return this.gender;
}
public void setParent(String parent)
{
this.parent = parent;
}
public String getParent()
{
return this.parent;
}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return this.id;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
}
答案 0 :(得分:1)
您正在打印由类的toString()方法返回的字符串。
您可以覆盖toString()并返回正确的值,否则您将需要实现自己的自定义适配器,或者使用正确的属性构建字符串数组,而不是将模型列表传递给arrayAdapter。
最简单的方法是覆盖toString()
public class ChildUser {
String childName;
String nickName, gender, parent;
int id, age;
@Override
public String toString()
{
return this.childName;
}
/*.......*/
}
希望这有帮助。