Android 通过自定义适配器将项目设置为 Spinner

Android set items into Spinner by custom Adapter

在我的应用程序中,我为 Spinner 自定义了 BaseAdapter,用于将列表中的数据插入其中,

在这种方法中,我可以 return 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 有一些数据 return 由 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 对话框没有数据而且是空的

在你的适配器中,你应该为你的 ArrayList 添加一个方法和变量(在这里添加你自己的对象)

public static ArrayList<ListObject> listItems;


public void addItems(ArrayList<ListObject> mListItems) {
  listItems = new ArrayList<ListObject>();
  listItems.clear();
  listItems.addAll(mListItems);
  notifyDataSetChanged();
}

然后在你的主要 class 调用中

SpinnerArrayAdapter.addItems(myList);

注意:确保在 getView 中,您的设置变量基于 class 中的 listItem,即。 title.setText(listItems.get(职位).姓名);

这里:

panel_numbers = (String[]) smsNumbers.toArray(new String[smsNumbers.size()]);

因为已经有List所以不需要将其转换为Array只需在SpinnerArrayAdapter的构造函数中添加一个参数即可 class :

List<String> list;
public SpinnerArrayAdapter(ActivitySmsSendGroup con,List<String> list) {
        mInflater = LayoutInflater.from(con);
        this.list=list;
    }

现在在 SpinnerArrayAdapter 中到处使用 list 而不是 panel_numbers class

当创建对象时,在 SpinnerArrayAdapter class 构造函数中传递来自 Activity 的列表:

SpinnerArrayAdapter dataAdapter = new SpinnerArrayAdapter(this,smsNumbers);

您的适配器应如下所示:

@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