[英]TextView inside of Spinner show com.jeanjn.guiadeacademia
I'm try to fill the spinner with a Custom ArrayAdapter. 我试图用自定义ArrayAdapter填充微调框。 I think the spinner is filled normal, but when the application try to open it, the spinner shows com.jeanjn.guiadeacademia...., but was to be show the text I inserted in the TextView on getView(...)
我认为微调框已正常填充,但是当应用程序尝试将其打开时,微调框将显示com.jeanjn.guiadeacademia ....,但将显示我在getView(...)上TextView中插入的文本
Obs: I'm not speak english very well, sorry. Obs:对不起,我的英语说得不太好。
Obs2: I did that, with a ListView, and worked very well. Obs2:我使用ListView做到了这一点,并且效果很好。 Now with Spinner...
现在有了Spinner ...
My code: 我的代码:
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;
}
The getDropDownView
method in ArrayAdapter
expects the items object to be the instance of CharSequence
if not it will set the item.toString()
to the Textview
. ArrayAdapter
的getDropDownView
方法期望items对象是CharSequence
的实例,否则,它将item.toString()
设置为Textview
。 That's why you are seeing the class name
(com.jeanjn.guiadeacademia) in spinner drop down items 这就是为什么您在微调框下拉项中看到
class name
(com.jeanjn.guiadeacademia)的原因
Since your item object is Categoria
, you need to override the getDropDownView
and provide the custom view like this, 由于您的商品对象是
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;
}
Also , change your custom Adapter super constructor call as 另外,将您的自定义Adapter超级构造函数调用更改为
super(context, R.layout.categoria_model, R.id.txvCategoria,itemsArrayList);
where R.layout.categoria_model is the custom layout and R.id.txvCategoria is the textview in the layout 其中R.layout.categoria_model是自定义布局,R.id.txvCategoria是布局中的textview
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.