在ListView Android的One Row中添加addView

时间:2014-10-30 03:09:40

标签: java android listview android-listview

在android中addView的一行中是否可以ListView?当用户点击ListView中的Button时,我尝试在ListView的一行中添加动态布局,但是我遇到了问题,新布局没有添加到我想要的行中,而是添加在几行中我不想要。这是一张图片Button位置: Image

这是我试图在ListView的一行中添加View之后的图片:

enter image description here 我不明白为什么会发生这种情况,这是我的代码:

  Adapter_Quisioner adapter_Quisioner = new Adapter_Quisioner(getActivity(), listQuisioner, lisLabelQues);
 list_tt.setAdapter(adapter_Quisioner);
   adapter_Quisioner.notifyDataSetChanged();

这是Adapter_Quisioner

    public class Adapter_Quisioner extends BaseAdapter implements OnClickListener{
    Context context;
    int count;
    ArrayList <ModelListQues>listQuisioner;
    ArrayList <ModelLabelQues>lisLabelQues;
    View view;

    public  Adapter_Quisioner(Context context, ArrayList <ModelListQues>listQuisioner, ArrayList <ModelLabelQues>lisLabelQues){
        this.context = context;
        this.listQuisioner = listQuisioner;
        this.lisLabelQues = lisLabelQues;
        this.count=listQuisioner.size();
    }

    @Override
    public int getCount() {

        return count;
    }
    @Override
    public int getViewTypeCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {

        return listQuisioner.get(position);
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

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

//       convertView = null;
        view= convertView;
        Holder holder= null;

        if (view==null){
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.adapter_questformat, parent,false);

            holder=new Holder();
            //linear layout
            holder.layout_label_kesehatan = (LinearLayout)view.findViewById(R.id.layout_label_kesehatan);
            holder.layout_jawaban_kesehatan = (LinearLayout)view.findViewById(R.id.layout_jawaban_kesehatan);
            holder.layout_label_pp = (LinearLayout)view.findViewById(R.id.layout_label_pp);
            holder.lay_inputtext_qu = (LinearLayout)view.findViewById(R.id.lay_inputtext_qu);
            holder.parentView_3 = (LinearLayout)view.findViewById(R.id.parentView_3);
            .............................................
            .............................................
            .............................................
            .............................................
            ............................................. //another view
            //Button
            holder.add_3 = (Button)view.findViewById(R.id.add_3);
            holder.add_5 = (Button)view.findViewById(R.id.add_5);
            holder.add_9a = (Button)view.findViewById(R.id.add_9a);
            holder.add_9b = (Button)view.findViewById(R.id.add_9b);
            holder.add_kes = (Button)view.findViewById(R.id.add_kes);

            view.setTag(holder);
        }
        else {
            holder=(Holder)view.getTag();
        }

        holder.add_3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                inflateNo3(1,position,view);

            }
        });


        return view;
    }

    private   class Holder {
        //linear layout utama 
        public LinearLayout layout_table_3, layout_table_5, layout_table_9a, layout_table_9b, layout_table_kes;
        //child layout
        public  LinearLayout parentView_3;
        public LinearLayout parentView_5;
        public LinearLayout parentView_9a;
        public LinearLayout parentView_9b;
        public LinearLayout parentView_kes;
        //button 
        public Button add_3, add_5, add_9a, add_9b, add_kes;

        private  View mExclusiveEmptyView;
    }
    private void inflateNo3(int int1, int position, View v) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final Holder holder=(Holder)v.getTag();
        final View dynamicview = inflater.inflate(R.layout.adapter_no3, null);
        TextView nomor_Rider =(TextView) dynamicview.findViewById(R.id.nomor_Rider);
        nomor_Rider.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {

                if (s.toString().isEmpty()) {

                    if (holder.mExclusiveEmptyView != null
                            && holder.mExclusiveEmptyView != dynamicview) {
                        holder.parentView_3.removeView(holder.mExclusiveEmptyView);
                    }
                    holder.mExclusiveEmptyView = dynamicview;
                } else {

                    if (holder.mExclusiveEmptyView == dynamicview) {
                        holder.mExclusiveEmptyView = null;
                    }
                }
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {}
            });
        holder.parentView_3.addView(dynamicview, holder.parentView_3.getChildCount() - 1);

    }

}

当我点击holder.add_3视图没有添加到我想要的行但是在另外几行中时,是否有某些内容丢失了?谢谢

0 个答案:

没有答案