微调自定义阵列适配器不允许选择项目 [英] Spinner with custom array adapter doesn't allow selecting item
问题描述
我用一个自定义的数组适配器好手。然而,在下拉列表中选择一个项目时,下拉列表撑在那里,微调没有更新,这是不当行为(相比,使用用绳子通用阵列适配器)。这是自定义类。我缺少什么?谢谢。
公共类CalendarNameAdapter扩展ArrayAdapter< AgendaLogic.ExternalCalendarInfo> {
上下文mContext;
ArrayList的< AgendaLogic.ExternalCalendarInfo> mCalendarLayers;
公共CalendarNameAdapter(上下文的背景下,INT资源的ArrayList< AgendaLogic.ExternalCalendarInfo>对象){
超级(上下文,资源,对象);
mContext =背景;
mCalendarLayers =物体;
} @覆盖
公共无效添加(AgendaLogic.ExternalCalendarInfo对象){
mCalendarLayers.add(对象);
} @覆盖
公众诠释的getCount(){
返回mCalendarLayers.size();
} 私有类ViewHolder {
Button按钮;
TextView中的TextView;
公共ViewHolder(按钮_btn,TextView的_tv){
按钮= _btn;
的TextView = _tv;
}
} 公共查看getView(INT位置,查看convertView,最终的ViewGroup父){
查看查看= convertView;
如果(查看== NULL){
鉴于= View.inflate(mContext,R.layout.li_calendar_display,NULL);
Button按钮=(按钮)view.findViewById(R.id.calColor);
TextView的名字=(TextView中)view.findViewById(R.id.calName);
view.setTag(新ViewHolder(按钮,名)); }
ViewHolder viewHolder =(ViewHolder)view.getTag();
viewHolder.button.setBackgroundColor(mCalendarLayers.get(位置)。颜色);
viewHolder.textView.setText(mCalendarLayers.get(位置)。名称); 返回视图。
} @覆盖
公共查看getDropDownView(最终诠释的立场,观点convertView,最终的ViewGroup父){
最后查看视图= getView(位置,convertView,父母); 返回视图。
}}
这是用法,首先初始化与模拟数据的微调。
公共无效initSpinner(){
CalendarNameAdapter exteralCalAdapter = createAdapterWithString(这一点,临时历);
mExternalSpinner.setAdapter(exteralCalAdapter); } 公共CalendarNameAdapter createAdapterWithString(上下文的背景下,字符串显示){
ArrayList的< AgendaLogic.ExternalCalendarInfo>名单=新的ArrayList< AgendaLogic.ExternalCalendarInfo>();
list.add(新AgendaLogic.ExternalCalendarInfo(-1,显示,0xaabbcc));
CalendarNameAdapter DataAdapter的=新CalendarNameAdapter(背景下,
0,清单); //不在乎资源文件,因为我们总是使用自定义文件,因此0
返回DataAdapter的;
}
当我已经载入我需要的数据,我将它添加到适配器:
私人无效setCalendarInfoToSpinner(ArrayList的< AgendaLogic.ExternalCalendarInfo> calList,微调微调){
CalendarNameAdapter适配器=(CalendarNameAdapter)spinner.getAdapter();
adapter.clear();
对于(AgendaLogic.ExternalCalendarInfo信息:calList)
adapter.add(信息);
adapter.notifyDataSetChanged();}
我最终回答我的问题供以后谁需要它。解决的办法是:检查列表项的布局文件。如果有一个按钮,无线电,复选框.etc。它可聚焦性设置为false,这样当你在项目挖掘,列表拿到水龙头,而不是子视图。
机器人:可聚焦=假
幸得另一种回答类似的问题,但似乎我现在不能找到它。
I use a custom array adapter for a spinner. However, when selecting an item in the dropdown list, the dropdown list stays there, and the spinner doesn't get updated, which is misbehaviour (compared to using a generic array adapter with string). This is the custom class. Am I missing anything? thanks.
public class CalendarNameAdapter extends ArrayAdapter<AgendaLogic.ExternalCalendarInfo> {
Context mContext ;
ArrayList<AgendaLogic.ExternalCalendarInfo> mCalendarLayers;
public CalendarNameAdapter(Context context, int resource, ArrayList<AgendaLogic.ExternalCalendarInfo> objects) {
super(context, resource, objects);
mContext = context;
mCalendarLayers = objects;
}
@Override
public void add(AgendaLogic.ExternalCalendarInfo object) {
mCalendarLayers.add(object);
}
@Override
public int getCount() {
return mCalendarLayers.size();
}
private class ViewHolder{
Button button;
TextView textView;
public ViewHolder(Button _btn, TextView _tv){
button = _btn;
textView = _tv;
}
}
public View getView(int position, View convertView, final ViewGroup parent) {
View view =convertView;
if (view==null){
view = View.inflate(mContext,R.layout.li_calendar_display, null);
Button button = (Button) view.findViewById(R.id.calColor);
TextView name = (TextView) view.findViewById(R.id.calName);
view.setTag(new ViewHolder(button, name));
}
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.button.setBackgroundColor(mCalendarLayers.get(position).color);
viewHolder.textView.setText(mCalendarLayers.get(position).name);
return view;
}
@Override
public View getDropDownView(final int position, View convertView, final ViewGroup parent) {
final View view = getView(position, convertView, parent);
return view;
}
}
This is the usage, first init the spinner with mock data.
public void initSpinner(){
CalendarNameAdapter exteralCalAdapter = createAdapterWithString(this, "temp calendar");
mExternalSpinner.setAdapter(exteralCalAdapter);
}
public CalendarNameAdapter createAdapterWithString(Context context, String display) {
ArrayList<AgendaLogic.ExternalCalendarInfo> list = new ArrayList<AgendaLogic.ExternalCalendarInfo>();
list.add(new AgendaLogic.ExternalCalendarInfo(-1, display, 0xaabbcc));
CalendarNameAdapter dataAdapter = new CalendarNameAdapter(context,
0, list); //don't care about resource file, since we always use custom file, hence 0
return dataAdapter;
}
When I already load the data I need, I add it to the adapter:
private void setCalendarInfoToSpinner(ArrayList<AgendaLogic.ExternalCalendarInfo> calList, Spinner spinner) {
CalendarNameAdapter adapter = (CalendarNameAdapter) spinner.getAdapter();
adapter.clear();
for (AgendaLogic.ExternalCalendarInfo info: calList)
adapter.add(info);
adapter.notifyDataSetChanged();
}
I ended up answering my own question for who needs it later. The solution is: check in the layout file of the list item. If there is a button, a radio, a checkbox, .etc. Set its focusability to false so that when you tap on the item, the list get the tap, not that child view.
android:focusable="false"
Credit goes to another answer for similar question, but I can't seem to find it now.
这篇关于微调自定义阵列适配器不允许选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!