android中列表视图中的布局

android中列表视图中的布局,android,Android,我有一个列表视图,在其中我添加了一些事件,动态地保存了一些图像。如果我将多个事件添加到同一行中,则根据事件的数量,应该创建多个布局并将其添加到列表视图的特定行中。它是如何做到这一点的。我试过这种方法。但当我向行中添加多个事件时,它与其他布局重叠。请帮帮我 public class EfficientAdapter extends BaseAdapter { public static final int POSITION_TAG_ID = 99; private LayoutIn

我有一个列表视图,在其中我添加了一些事件,动态地保存了一些图像。如果我将多个事件添加到同一行中,则根据事件的数量,应该创建多个布局并将其添加到列表视图的特定行中。它是如何做到这一点的。我试过这种方法。但当我向行中添加多个事件时,它与其他布局重叠。请帮帮我

public class EfficientAdapter extends BaseAdapter {
    public static final int POSITION_TAG_ID = 99;
    private LayoutInflater mInflater;

    private List<DTO> calendarList;
    long time=0;
    Context context;


    int i;
    /** Holds the Date's Day for the current context */
    private Date cntxtDate;
    private OnClickListener eventClickListner = null;
    private OnClickListener timeSlotClickListner = null;

    public EfficientAdapter(Date contextDate, int filter, Context context) {
        this.cntxtDate = contextDate;
        this.mInflater = LayoutInflater.from(context);
        this.context = context;

        this.calendarList = CalendarDAO.getInstance().getRecordsWithinBounds(
                Utilities
                        .convertToString(new Date(contextDate.getYear(),
                                contextDate.getMonth(), contextDate.getDate(),
                                0, 0, 0)),
                Utilities.convertToString(new Date(contextDate.getYear(),
                        contextDate.getMonth(), contextDate.getDate(), 23, 59,
                        59)), filter, getDBObject(0));
    }

    public List<DTO> getDTos() {
        return calendarList;
    }

    public int getCount() {
        return 48;
    }

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

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        long differencestart;
        Date startDate, endDate;


        Date currViewDate = new Date(cntxtDate.getYear(), cntxtDate.getMonth(),
                cntxtDate.getDate(), position / 2, (position % 2) * 30, 0);

        convertView = mInflater.inflate(R.layout.listview, null);
        //layout = layoutInflater.inflate(R.layout.listviewinflate, null);
        Holder holder = new Holder();
        holder = new Holder();

        holder.timeSlot = (TextView) convertView
                .findViewById(R.id.TextViewLeft);
        //LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        holder.eventColor = (RelativeLayout) convertView.findViewById(R.id.Right);      


        holder.eventTitle = (TextView) convertView
                .findViewById(R.id.TextViewRight);
        holder.eventType = (ImageButton) convertView
                .findViewById(R.id.chooseactivity);
        holder.eventLayout = (RelativeLayout) convertView
                .findViewById(R.id.textboxlayout);
        TextView startTime = (TextView) convertView
                .findViewById(R.id.calDayViewStartTime);
        TextView endTime = (TextView) convertView
                .findViewById(R.id.calDayViewEndTime);


        convertView.setTag(new Integer(position));
        convertView.setOnClickListener(timeSlotClickListner);

        holder.timeSlot.setText(CalendarEvents.timeFormatter
                .format(currViewDate));
        if (position % 2 == 1) {
            holder.timeSlot.setTextColor(Color.LTGRAY);
        }

        CalendarDTO objCalendarDTO;
        for (DTO dto : calendarList) {
            objCalendarDTO = (CalendarDTO) dto;
            startDate = Utilities.convertToDate(objCalendarDTO.startTime);
            endDate = Utilities.convertToDate(objCalendarDTO.endTime);

            differencestart = (startDate.getTime() - currViewDate.getTime());   
            if ((currViewDate.getTime() >= startDate.getTime() && currViewDate
                    .getTime() < endDate.getTime())
                    || (differencestart < CalendarEvents.ThirtyMinutesInMiliseconds && differencestart >= 0)) {
                LinearLayout l = new LinearLayout(context);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                params.setMargins(87, 0, 0, 0);
                if(time == startDate.getTime()) {

                    l.setPadding(90*2, 0, 0, 0);    




                }
                else {

                    l.setPadding(90, 0, 0, 0);  


                }
                l.setLayoutParams(params);
                holder.eventLayout.removeView(l);
                holder.eventLayout.addView(l);
                holder.eventLayout.setVisibility(View.VISIBLE);
                holder.eventLayout.setTag(new Integer(objCalendarDTO.id));
                if ((differencestart < CalendarEvents.ThirtyMinutesInMiliseconds && differencestart >= 0)) {

                    holder.eventType.setVisibility(View.VISIBLE);
                    String text = CalendarEvents.timeFormatter.format(Utilities
                            .convertToDate(objCalendarDTO.startTime));
                    startTime.setText(text);
                    text = CalendarEvents.timeFormatter.format(Utilities
                            .convertToDate(objCalendarDTO.endTime));
                    endTime.setText(text);
                    holder.eventTitle.setText(objCalendarDTO.description);
                    time =  startDate.getTime();
                }
                if (objCalendarDTO.type == 2) {
                    holder.eventColor.setBackgroundColor(0x44801800);
                    holder.eventType
                            .setBackgroundDrawable((Drawable) AspireApplication
                                    .getResourcesObject().getDrawable(
                                            R.drawable.personalactivity));
                } else if (objCalendarDTO.type == 0) {
                    holder.eventColor.setBackgroundColor(0x440000FF);
                    holder.eventType
                            .setBackgroundDrawable((Drawable) AspireApplication
                                    .getResourcesObject().getDrawable(
                                            R.drawable.aspireactivity));
                } else if (objCalendarDTO.type == 1) {
                    holder.eventColor.setBackgroundColor(0x4400FF00);
                    holder.eventType
                            .setBackgroundDrawable((Drawable) AspireApplication
                                    .getResourcesObject().getDrawable(
                                            R.drawable.schoolactivity));
                }

                /* tell who is going to listen for clicks on the appointments */
                holder.eventLayout.setOnClickListener(this.eventClickListner);

            }

        }
        return convertView;
    }

    class Holder {
        public TextView timeSlot;
        public RelativeLayout eventColor;
        public TextView eventTitle;
        public ImageButton eventType;
        public RelativeLayout eventLayout;
        public CheckBox checkbox;
        public RelativeLayout textboxlayout;



    }

我真的不明白你在说什么,但你应该能够在每一行中使用LinearLayout或其他视图组,这样你就可以添加多个子项。只是要小心;回收行时,您需要在视图组上调用removeAllViews,以便获得新的布局以添加子视图。

ya在添加任何视图之前,我已删除视图。如果我使用相对布局而不是线性布局,那会怎么样呢。。实际上,我的主要目标是在列表视图中动态地向一行添加布局。啊,我明白你的意思了。我认为最好完全在getView中完成工作,然后在数据更改时调用invalidate,从而再次调用getView。否则,上下滚动时可能会丢失添加的视图。这根本不起作用。。我保存了所有视图。但在多个图像的特定行中,它们是重叠的。我想把它们并排展示。然后你可能应该使用LinearLayout