java – How to add an item in Listview adapter Linear Layout-ThrowExceptions

Exception or error:

I know how to populate listview and I know how to make custom adapter.
I want to add View inside Linear layout of rowLayout of ListView.
Here is MyList Adapter.

I want a listview which each item layout is created runtime But I can not achieve this.

This code is working without ListView but when I add it to Linearlayout inside listview .It Gives me error.

public class KmapListAdapter extends BaseAdapter  {

    Context context;
    LayoutInflater layoutInflater;
    List<KmapModel> mOriginalValues = new ArrayList<KmapModel>();


    public KmapListAdapter(Context activity, List<KmapModel> kmapModelList) {

        this.context = activity;
        this.mOriginalValues = kmapModelList;

    }

    @Override
    public int getCount() {
if(mOriginalValues == null)
    return  0;
        return mOriginalValues.size();
    }

    @Override
    public Object getItem(int position) {

        return mOriginalValues.get(position);
    }

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

    ViewHolder Holder ;

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

        KmapModel kmapModel;
        if (layoutInflater == null)
            layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            Holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.rowlayout, null);
            Holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linear_layout_matrix_1);

            convertView.setTag(Holder);

        } else
            Holder = (ViewHolder) convertView.getTag();


        kmapModel = mOriginalValues.get(position);
        Holder.linearLayout.addView(kmapModel.getLinearLayout());
        return convertView;
    }




    static class ViewHolder {
        LinearLayout linearLayout;}}

And it is my Model List

 public List<KmapModel> ListViewOneItem( int ROWS, int COLS) {

        String[][] KmapArray = KMapMaker(ROWS, COLS);
        //  Log.v("Kmap array",""+KmapArray);
        TextView[][] KmapTextView = new TextView[KmapArray.length][KmapArray[0].length];

        int temp = 3;
        for (int i = 0; i < KmapArray.length; i++) {
            KmapModel kmapModel = new KmapModel();
            LinearLayout rowLinearLayout = makeRowLinearLayout();


            for (int j = 0; j < KmapArray[0].length; j++) {

                TextView rowTextView = makeTextView();
                rowTextView.setPadding(4, 4, 4, 4);


                rowLinearLayout.addView(rowTextView);
                KmapTextView[i][j] = rowTextView;// Add a text view in a jagged array for later use

            }
            kmapModel.setLinearLayout(rowLinearLayout);
            kmapModelList.add(kmapModel);


        }
        return  kmapModelList;
    }

Now I got an Error

The specified child already has a parent. You must call removeView() on the child’s parent first.
on this line

 Holder.linearLayout.addView(kmapModel.getLinearLayout());

Q1) How to resolve this Problem
Q2) Why I am getting this issue?

Thanks @pskink he give me a way I try Then I upload Solution.

How to solve:

The specified child already has a parent.

Stop trying to give the child a second parent. All views are from broken homes. They have one parent.

You can add views to views, you cannot add views that are already added. This causes loops and loops are loopy. And to successfully iterate the viewtree it needs to be a tree. So views get one and only one parent. Either clear out the layout you already have and use that or add a new view to the layout or basically anything you want that doesn’t involve using the same object in several places in the view tree.

###

Now Finally I found A good Solution After Resaeaching.

Thanks everyone Who contribute.
This my List Adapter and I get what i want

public class KmapListAdapter extends BaseAdapter {

    Activity context;
    LayoutInflater layoutInflater;
    List<KmapModel> mOriginalValues;


    public KmapListAdapter(Activity activity, List<KmapModel> kmapModelList) {

        this.context = activity;
        this.mOriginalValues = kmapModelList;

    }

    @Override
    public int getCount() {

        Log.d("SIZEOF",""+mOriginalValues.size());
        return mOriginalValues.size();
    }

    @Override
    public Object getItem(int position) {

        return mOriginalValues.get(position);
    }

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

    ViewHolder Holder;

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


        if (layoutInflater == null)
            layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            Holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.rowlayout, null);
            Holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.linear_layout_matrix);

            convertView.setTag(Holder);



        LinearLayout rowLinearLayout = makeRowLinearLayout();
        Holder.linearLayout.addView(rowLinearLayout);
        KmapModel kmapModel = new KmapModel();
         kmapModel  = mOriginalValues.get(position);
         String[] array = kmapModel.getKmapString();
        for (int i=0 ; i< array.length;i++)
        {
            TextView rowTextView = makeTextView();
            rowTextView.setPadding(4, 4, 4, 4);
            rowTextView.setText(""+array[i]);
           rowLinearLayout.addView(rowTextView);

        }




        return convertView;
    }

    public TextView makeTextView() {
        LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, 80);
        lparams.setMargins(7, 0, 7, 0);

        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setLayoutParams(lparams);
        tv.setTextSize((float) 17);
        tv.setMaxLines(10);

        tv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.edittext_selector_background));
        return tv;
    }


    public LinearLayout makeRowLinearLayout() {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);


        return linearLayout;
    } // End of makeRowLinearLayout()

    static class ViewHolder {
        LinearLayout linearLayout;


    }


}

Edit

We can’t add a view to a view that is already added somewhere. Remove the old view, and add a new view, or reuse the old view by removing all the stuff from it. But, there is no adding an already added view to the viewtree again.

Leave a Reply

Your email address will not be published. Required fields are marked *