如何在Listview中动态添加LinearLayout?

时间:2018-05-02 13:21:49

标签: android listview adapter

我使用自定义适配器创建了listview,我希望根据数据库条目动态添加以下结构。

LinearLayout (Vertical)
TextView 
LinearLayout (Horizontal)
TextView 
TextView  

----这个文本视图将是动态的,可以是2,3或更多,具体取决于数据库

我编写了以下代码,只是为了动态显示结构。

@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;
}

请告诉我为什么linearlayout没有显示 我试过了:

convertView.addView(LinearLayout);

但是这不起作用,它在运行时不会在屏幕上显示线性布局。

1 个答案:

答案 0 :(得分:1)

首先调用TimeLinearLayout.removeAllViews();然后尝试添加任何其他视图