我尝试使用Custom ArrayAdapter填充微调器。我认为微调器正常填充,但是当应用程序尝试打开它时,微调器显示com.jeanjn.guiadeacademia ....,但是要显示我在getView(...)上的TextView中插入的文本< / p>
Obs:我不会说英语,抱歉。
Obs2:我用ListView做到了,效果很好。现在有了Spinner ......
我的代码:
private void carregarSpinner() {
Spinner spinner = (Spinner) findViewById(R.id.CateSpinner);
DataBase db = new DataBase(this);
Cursor cursor = db.getData("SELECT * FROM Categoria");
ArrayList<Categoria> txts = new ArrayList<Categoria>();
while(cursor.moveToNext())
{
Categoria ex = new Categoria();
ex.setNome(cursor.getString(1));
ex.setId(cursor.getInt(0));
txts.add(ex);
}
AdapterCategoria adapter = new AdapterCategoria(this, txts);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
db.closeDB(cursor);
}
public class AdapterCategoria extends ArrayAdapter<Categoria> {
private final Context context;
private final ArrayList<Categoria> itemsArrayList;
public AdapterCategoria(Context context, ArrayList<Categoria> itemsArrayList) {
super(context, R.id.txvCategoria, itemsArrayList);
this.context = context;
this.itemsArrayList = itemsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.categoria_model, parent, false);
// 3. Get the two text view from the rowView
TextView labelView = (TextView) rowView.findViewById(R.id.txvCategoria);
// 4. Set the text for textView
labelView.setText(itemsArrayList.get(position).getNome());
labelView.setTag(itemsArrayList.get(position).getId());
// 5. retrn rowView
return rowView;
}
答案 0 :(得分:0)
getDropDownView
中的ArrayAdapter
方法希望items对象成为CharSequence
的实例,否则会将item.toString()
设置为Textview
。这就是为什么你在微调器下拉物品中看到class name
(com.jeanjn.guiadeacademia)的原因
由于您的商品对象为Categoria
,因此您需要覆盖getDropDownView
并提供此类自定义视图,
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
TextView row = (TextView) super.getView(position, convertView, parent);
row.setText(getItem(position).getNome());
return row;
}
此外,将您的自定义适配器超级构造函数调用更改为
super(context, R.layout.categoria_model, R.id.txvCategoria,itemsArrayList);
其中R.layout.categoria_model是自定义布局,而R.id.txvCategoria是布局中的textview