在我的应用程序中,我为BaseAdapter
定制Spinner
,用于将数据从List插入到
在这个方法中,我可以将List
作为数据返回:
public List<String> getSmsNumberList() {
return Arrays.asList(smsNumbers.split("\\s*,\\s*"));
}
在确定成功数据后,我准备插入Spinner:
smsNumbers = G.getInfo().getSmsNumberList();
panel_numbers = (String[]) smsNumbers.toArray(new String[smsNumbers.size()]);
SpinnerArrayAdapter dataAdapter = new SpinnerArrayAdapter(this);
spinner_panel_numbers.setAdapter(dataAdapter);
smsNumbers
包含getSmsNumberList()
函数返回的一些数据,现在我在自定义BaseAdapter下使用此数据将视图数据转换为Spinner
:
适配器代码已更新
private class SpinnerArrayAdapter extends BaseAdapter {
private final LayoutInflater mInflater;
List<String> list;
public SpinnerArrayAdapter(ActivitySmsSendGroup con, List<String> list) {
mInflater = LayoutInflater.from(con);
this.list = list;
}
@Override
public int getCount() {
return list.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.panel_number_spinner_style, null);
holder = new ListContent();
holder.name = (TextView) v.findViewById(R.id.textView1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.name.setText(list[position] + "");
return v;
}
}
static class ListContent {
TextView name;
}
遗憾的是,我的Spinner
对话框没有数据且空白
答案 0 :(得分:0)
在你的适配器中,你应该为你的ArrayList添加一个方法和变量(在这里添加你自己的对象)
public static ArrayList<ListObject> listItems;
public void addItems(ArrayList<ListObject> mListItems) {
listItems = new ArrayList<ListObject>();
listItems.clear();
listItems.addAll(mListItems);
notifyDataSetChanged();
}
然后在你的主课堂电话
SpinnerArrayAdapter.addItems(myList);
注意:在getView中确保您的&#e; e设置变量基于类中的listItem即。 title.setText(listItems.get(位置)。名称);
答案 1 :(得分:0)
下面:
panel_numbers =(String [])smsNumbers.toArray(new 串[smsNumbers.size()]);
因为已经有List所以不需要将它转换为Array只需要向SpinnerArrayAdapter
类的构造函数添加一个参数:
List<String> list;
public SpinnerArrayAdapter(ActivitySmsSendGroup con,List<String> list) {
mInflater = LayoutInflater.from(con);
this.list=list;
}
现在在SpinnerArrayAdapter类中使用list
代替panel_numbers
创建对象时,从SpinnerArrayAdapter
类构造函数中的Activity传递List:
SpinnerArrayAdapter dataAdapter = new SpinnerArrayAdapter(this,smsNumbers);
答案 2 :(得分:0)
您的适配器应如下所示:
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent){
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView, ViewGroup parent){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater.inflate(R.layout.spinner_row, parent, false);
spnItemName = (TextView) rowView.findViewById(R.id.spnItemName);
spnItemDel = (TextView) rowView.findViewById(R.id.spnItemDel);
spnItemName.setText(iName.get(position)+"");
spnItemDel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
//iName[position] = null;
iName.remove(position);
notifyDataSetChanged();
}
});
return rowView;
}
有关完整源代码,请参阅我的github project