从Custom BaseAdapter为ListView动态添加元素

时间:2015-02-03 13:54:56

标签: android listview baseadapter

我有这个自定义BaseAdapter。在一排我有3个微调器。我希望在一行中的每个微调器都有一个选定的项目之后,另一行以动态方式添加到列表视图中并显示。我怎样才能做到这一点?这就是我的尝试。

public MateriiAdapter (Activity a, ArrayList<YTOMaterie> orar) {
        this.orar = orar;
        activity = a;

        inflater = ( LayoutInflater )activity.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        if(orar.size()<=0)
            return 1;
        return orar.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder{
        public NoDefaultSpinner spinnerOra;
        public NoDefaultSpinner spinnerMinute;
        public NoDefaultSpinner spinnerMaterii;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        ViewHolder holder;

        if(convertView==null){

            vi = inflater.inflate(R.layout.item_materie, null);

            holder = new ViewHolder();
            holder.spinnerOra = (NoDefaultSpinner) vi.findViewById(R.id.spOra);
            holder.spinnerMinute = (NoDefaultSpinner) vi.findViewById(R.id.spMinute);
            holder.spinnerMaterii = (NoDefaultSpinner) vi.findViewById(R.id.spMaterii);

            final boolean[] oraSelectata = {false};
            final boolean[] minSelectat={false};
            final boolean[] materieSelectata={false};

            holder.spinnerOra.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                    oraSelectata[0] = true;
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

            holder.spinnerMinute.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                    minSelectat[0] = true;

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

            holder.spinnerMaterii.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                    materieSelectata[0] = true;
                    if(materieSelectata[0] == true && oraSelectata[0] == true
                            && minSelectat[0] == true) {
                        YTOMaterie y =  new YTOMaterie();
                        orar.add(y);
                        notifyDataSetChanged();
                        materieSelectata[0] = false;
                        oraSelectata[0] = false;
                        minSelectat[0] = false;
                    }

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }
            });

            vi.setTag( holder );
        }
        else
            holder=(ViewHolder)vi.getTag();

        if(orar.size()<=0) {
        } else {
            tempValue=null;
            tempValue = ( YTOMaterie ) orar.get( position );





        }
        return vi;
    }

1 个答案:

答案 0 :(得分:-1)

好吧我已经举了两个简单的Spinner的例子,它们动态地相互依赖,这里是代码,你可以采取你想要的多个微调器,并动态更改onItemSelectedListener()回复的值,如果你没有得到你想要的答案..

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    course=new ArrayList<String>();
    techbranch=new ArrayList<String>();
    lawbranch=new ArrayList<String>();
    branch=new ArrayList<String>();


    course.add("Course");
    course.add("Tech");
    course.add("Law");


    techbranch.add("CS");
    techbranch.add("IT");
    techbranch.add("ME");
    techbranch.add("ECE");


    lawbranch.add("CRIME LAW");
    lawbranch.add("LABOUR LAW");
    lawbranch.add("CORPORATE Law");
    lawbranch.add("BUSINESS Law");

    spinner1=(Spinner)findViewById(R.id.spinner1);
    spinner2=(Spinner)findViewById(R.id.spinner2);

    adapter1=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line,course);
    spinner1.setAdapter(adapter1);

    adapter2=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, branch);
    spinner2.setAdapter(adapter2);

    spinner1.setOnItemSelectedListener(this);


}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos,
        long id) {

    switch(pos)
    {
    case 1:
        branch.clear();
        branch.addAll(techbranch);
        adapter2.notifyDataSetChanged();
        break;
    case 2:
        branch.clear();
        branch.addAll(lawbranch);
        adapter2.notifyDataSetChanged();
        break;

    }
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {



}