在Android中使用分隔在同一个微调器中添加两个数组列表

时间:2017-12-14 04:39:19

标签: android arraylist android-spinner dropdown

我想在同一个微调器中添加两个数组列表,当我点击spinner时,它应该显示两个用标签分隔的列表,这样我就可以从同一个{{1}中的任何列表中选择项目}。我怎样才能在spinner中实现这一点。

目前我这样做,这是正确的方法吗?

android

1 个答案:

答案 0 :(得分:0)

对于这个问题,你需要创建三个你可以根据它使用的arraylist:

ArrayList<String> arrayOne=new ArrayList<String>();
for(int i=0;i<=5;i++)
{
arrayOne.add(" "+i);
}

ArrayList<String> arrayTwo=new ArrayList<String>();
for(int j=0;j<=5;j++)
{
arrayTwo.add(" "+j);
}

after that you should merge both arraylist into one  using this:


ArrayList<String> arrayFinal=new ArrayList<String>();
 for(k=0;k<arrayOne.size();k++)
{
arrayFinal.add(arrayOne.get(k));
}

for(u=0;u<arrayTwo.size();u++)
{
arrayFinal.add(arrayTwo.get(u));
}

现在你可以找到一个合并两个arraylist数据的单个arraylist,现在你可以通过最终的arraylist数据初始化你的微调器。

制作一个自定义适配器,用于发送要禁用标签的位置的位置 用这个:

public class CustomAdapter extends BaseAdapter {

    List<String> ayylist;
    Context context;
int postionDisable;


    public CustomAdapter(Context context, List<String> ayylist,int postionDisable) {
        this.ayylist = ayylist;
        this.context = context;
        this.postionDisable=postionDisable;
    }


    @Override
    public int getCount() {
        return ayylist.size();
    }

    @Override
    public Object getItem(int position) {
        return ayylist.get(position);
    }

    public boolean areAllItemsEnabled() {
        return false;
    }

    public boolean isEnabled(int position) {
        if (position == postionDisable) {
            return false;
        } else return true;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.simpletext1, parent, false);
        TextView textView = (TextView) convertView.findViewById(R.id.TextView);
        textView.setText(ayylist.get(position).toString());
        textView.setBackgroundColor(context.getResources().getColor(R.color.whiteColor));
        if (position == postionDisable) {
            textView.setTextColor(context.getResources().getColor(R.color.blackcolor));
            textView.setTextSize(15);
        }

        return convertView;
    }
}