我有一个微调器,让用户从xml中的一组字符串数组中选择一顿饭。用户选择项目后,我将选择放入数据库,并显示在Listview上。
在我的模型中,我有一个toString(),
@Override
public String toString() {
return id + " " + note +"\n meal: " + this.meal;
}
在用于编辑膳食的控制器类中,我有这个,
public class EditNoteFragment extends Fragment implements
OnItemSelectedListener{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_edit,
container, false);
// populating the spinner with string array of time category
spinner = (Spinner) rootView.findViewById(R.id.spinner_meal_category);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity().getApplicationContext(), R.array.meal_array,
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
meal = parent.getItemAtPosition(pos).toString();
Log.i(LOGTAG, "Got the meal: " + meal);
}
public boolean onOptionsItemSelected(MenuItem item) {
// I added the field variable meal into the datebase in this block of code
//note.setMeal(meal.toString());
}
}
我使用log验证我得到了用户选择并存储在字段变量&#34; meal&#34;在我的课堂上,但是当我运行应用程序并且列表显示为空时。
我确信我已成功将这顿饭添加到数据库中。
这是视图类
public static class NoteFragment extends ListFragment {
TextView tx_list_note;
NoteDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
datasource = new NotesDataSource(getActivity());
datasource.open();
List<Note> notes = datasource.findAll();
if (notes.size() == 0) {
createData();
notes = datasource.findAll();
}
ArrayAdapter<Note> adapter = new ArrayAdapter<Note>(getActivity(),
android.R.layout.simple_list_item_1, notes);
setListAdapter(adapter);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notes,
container, false);
return rootView;
}
}
}
在fragment_notes.xml中,我在相对布局中有一个listview<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignParentLeft="true"
>
</ListView>
非常感谢你提前帮助。任何建议将不胜感激。