简体   繁体   中英

How to dynamically add LinearLayout in a Listview?

I have created a listview with Custom Adapter, I wish to add following structure dynamically depending on database entry.

LinearLayout (Vertical)
TextView 
LinearLayout (Horizontal)
TextView 
TextView  

---- this text view will be dynamic can be 2,3 or more depending on the database

I have written following code just to display the structure dynamically.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    final ViewHolder holder;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.cinema_info_item, null);
        //convertView = mInflater.inflate(R.layout.cinema_info_item, parent, false);
        holder = new ViewHolder();

        holder.imageIV = (ImageView) convertView.findViewById(R.id.cinema_info_item_iv);
        holder.movieNameTv = (TextView) convertView.findViewById(R.id.cinema_info_item_movie_name_tv);
        holder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.relativeLayout);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
        // add this linearlayout dynamically for each listview item
        LinearLayout parent_linear = new LinearLayout(activity);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(20, 5, 20, 5);
        parent_linear.setLayoutParams(layoutParams);
        parent_linear.setOrientation(LinearLayout.VERTICAL);
        parent_linear.setBackgroundColor(Color.BLACK);

        //children of parent linear layout
        TextView tvStatus = new TextView(activity);
        tvStatus.setText("status");

        LinearLayout TimeLinearLayout = new LinearLayout(activity);

        TimeLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        TimeLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        parent_linear.setBackgroundColor(Color.BLUE);

        parent_linear.removeAllViews();
        parent_linear.addView(tvStatus);
        parent_linear.addView(TimeLinearLayout);

        //children of layout2 LinearLayout
        TextView tv1 = new TextView(activity);
        tv1.setText("1");
        TextView tv2 = new TextView(activity);
        tv1.setText("2");
        TextView tv3 = new TextView(activity);
        tv1.setText("3");
        TextView tv4 = new TextView(activity);
        tv1.setText("4");

        TimeLinearLayout.removeAllViews();
        TimeLinearLayout.addView(tv1);
        TimeLinearLayout.addView(tv2);
        TimeLinearLayout.addView(tv3);
        TimeLinearLayout.addView(tv4);

    return convertView;
}

Please tell me why linearlayout is not getting display
I have tried :

convertView.addView(LinearLayout);

But this does not work, it does not display the linear layout on screen when run.

First call TimeLinearLayout.removeAllViews(); then try to add any other view

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM