我正在为大学做一个项目,我有一个DialogFragment,我在那里为数据库记录一个新的动物。在表单中,有一个从数据库加载的微调器。这是DialogFragment的“onCreate”代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.dialog_add_animal, null);
Spinner spEspecies = (Spinner) v.findViewById(R.id.spinner);
Cursor c = getActivity().getContentResolver().query(ContratoBBDD.Especies.URI_CONTENIDO,null,null,null,null);
SimpleCursorAdapter adaptador =
new SimpleCursorAdapter(getActivity().getApplicationContext() ,android.R.layout.simple_spinner_item,c,
new String[]{ContratoBBDD.Especies._id},
new int [] {android.R.id.text1},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
Log.d("Tam", c.getCount() + "");
Log.d("Col", c.getColumnCount() + "");
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEspecies.setAdapter(adaptador);
ActionBar actionBar = ((AppCompatActivity) getActivity())
.getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDefaultDisplayHomeAsUpEnabled(false);
}
我可以在日志和调试中看到光标有信息,旋转器也是如此 - 但是当我在微调器中单击时,它是空的。我在哪里丢失信息?
答案 0 :(得分:0)
您可以查看此链接
Putting cursor data into an array
//the constructor
Spinner spEspecies = (Spinner) v.findViewById(R.id.spinner);
//set the value to show
//change this line with query, better use ArrayList<String>
String Especies[] = {"Homo Sapiens","Felis Domestica","Zea mays"};
//insert to spinner
ArrayAdapter<String> aaspEspecies = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Especies);
// The drop down view
aaspEspecies.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEspecies.setAdapter(aaspEspecies);
答案 1 :(得分:0)
谢谢你的回答。我在其他帖子上做了你对我说的更改,我得到了微调器加载数组数据。我使用过这段代码:
Cursor c = getActivity().getContentResolver().query(ContratoBBDD.Especies.URI_CONTENIDO,null,null,null,null);
ArrayList<String> especies = new ArrayList<String>();
c.moveToFirst();
while(!c.isAfterLast()) {
especies.add(c.getString(c.getColumnIndex("descripcion")));
c.moveToNext();
}
c.close();
ArrayAdapter<String> aaspEspecies = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item,especies);
// The drop down view
aaspEspecies.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spEspecies.setAdapter(aaspEspecies);
当我调试应用程序时,微调器是好的,但是当我点击它时,数据不会显示。