Android development: dynamically generate TextView

Maybe friends will encounter such a requirement in the process of Android development: the need to display list information dynamically; here I take the conference information publishing screen I developed recently as an example.

Then the meeting reservation publishing screen needs to display the meeting reservation information of the day. The problem is that I don't know whether there is a meeting reservation on this day, so I don't know how many TextView tags are needed. If it is ordinary Java+jsp or Vue development, this requirement is very simple; the former only needs to introduce the jstl tag library and cooperate with the el expression to easily complete the requirement, and the latter can also easily meet the requirement by using ElementUI. So in Android development, how should this effect be achieved? This is the <ListView> tag I shared with you today! Without further ado, let's go to the code!

1. Add in the layout file that needs to dynamically generate the TextView label

 If you don’t want to tap by hand, you can directly select the Design view mode in the upper right corner and drag to adjust

2. Create a new list_view.xml file (the name doesn’t matter, you can customize it), and set the layout of TextView in it

 

 Same as above, if you don’t want to touch it by hand, you can directly open the Design view mode adjustment, you can add as many TextViews as you want, according to your own page layout

3. Traverse the required data in the onGreat method of the .java file of your dynamically loaded layout file

@Override
    public void showResponse(List<TCrConferenceReservation> result) {
        Date date = new Date();
        //动态加载TextView
        ListView listView = (ListView) findViewById(R.id.list);
        List<ConferenceReservationVO> list = new ArrayList<>();
        for (TCrConferenceReservation c : result) {
            if (DateUtils.isToday(DateUtils.convertDate(c.getStartTime(),"yyyy-MM-dd HH:mm").getTime()) && (DateUtils.convertDate(c.getEndTime(),"yyyy-MM-dd HH:mm").getTime() > date.getTime())){
            ConferenceReservationVO conferenceReservationVO = new ConferenceReservationVO();
            conferenceReservationVO.setId(c.getId());
            conferenceReservationVO.setName(c.getName());
            conferenceReservationVO.setIntroduction(c.getIntroduction());
            conferenceReservationVO.setEmployeeName(c.getEmployeeName());
            conferenceReservationVO.setDepartmentName(c.getDepartmentName());
            conferenceReservationVO.setCreateTime(c.getCreateTime());
            conferenceReservationVO.setStartTime(c.getStartTime());
            conferenceReservationVO.setEndTime(c.getEndTime());
            String startTime = c.getStartTime().substring(11, 16);
            String endTime = c.getEndTime().substring(11, 16);
            String times = startTime + "-" + endTime;
            conferenceReservationVO.setTimeQuantum(times);
            String conInFo = times + " | " + c.getEmployeeName() + " | " + c.getDepartmentName();
            conferenceReservationVO.setConferenceInformation(conInFo);
            list.add(conferenceReservationVO);
            }
        }
        //预约会议时间段
        TextView conferenceTime = (TextView) findViewById(R.id.conferenceTime);
        //预约会议名
        TextView conferenceName = (TextView) findViewById(R.id.conferenceName);
        //预约会议人
        TextView employeeName = (TextView) findViewById(R.id.employeeName);
        //部门名
        TextView departmentName = (TextView) findViewById(R.id.departmentName);
        //当前时间
        TextView currentTime = (TextView) findViewById(R.id.currentTime);
        //会议状态
        TextView fokus = (TextView) findViewById(R.id.fokus);
        //分割线
        TextView border = (TextView) findViewById(R.id.border);
        //今日会议数
        TextView count = (TextView) findViewById(R.id.count);
        if (list.size() > 0) {
            if (date.getTime() < DateUtils.convertDate(list.get(0).getStartTime(),"yyyy-MM-dd HH:mm").getTime()){
                fokus.setText("下一个会议");
                border.setBackgroundColor(Color.parseColor("#FB9F4A"));
            }
            conferenceTime.setText(list.get(0).getTimeQuantum());
            conferenceName.setText(list.get(0).getName());
            employeeName.setText(list.get(0).getEmployeeName());
            departmentName.setText(list.get(0).getDepartmentName());
            currentTime.setText(DateUtils.Format(date, "yyyy/MM/dd HH:mm"));
            count.setText("今日会议 " + list.size());
        }else {
            list.add(new ConferenceReservationVO());
            fokus.setText("会议空闲中");
            border.setBackgroundColor(Color.WHITE);
            conferenceName.setText("暂无会议");
            conferenceTime.setText("无");
            employeeName.setText("无");
            departmentName.setText("无");
            count.setText("今日会议 " + list.size());
        }
        listView.setAdapter(new MyAdapter(list,this));
    }

I am here because I added a database in the app, so I didn’t write it in the onGreat method. If you are interested in introducing a database, I will post a blog to share with you later!

4. Create a new MyAdapter class to inherit from the BaseAdapter class (the name is defined by yourself)

public class MyAdapter extends BaseAdapter {

    private List<ConferenceReservationVO> data;
    private Context context;
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public View getView(int i, View con, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        View view;
        Date date = new Date();
        if (con == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_view,null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            view = con;
            viewHolder = (ViewHolder) view.getTag();
        }
        if (data.get(i).getTimeQuantum() != null) {
            viewHolder.textView.setText(data.get(i).getName());
            viewHolder.info.setText(data.get(i).getConferenceInformation());
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.textView.getLayoutParams();
            lp.topMargin = 40;
            if (i == 0) {
                if ((DateUtils.convertDate(data.get(0).getStartTime(),"yyyy-MM-dd HH:mm").getTime() < date.getTime()) && (DateUtils.convertDate(data.get(0).getEndTime(),"yyyy-MM-dd HH:mm").getTime() > date.getTime())){
                    System.out.println(DateUtils.convertDate(data.get(0).getEndTime(),"yyyy-MM-dd HH:mm").getTime() > date.getTime());
                    System.out.println("data.get(0).getEndTime() = " + data.get(0).getEndTime());
                    viewHolder.tv.setBackgroundColor(Color.parseColor("#5DC755"));
                }else if ((DateUtils.convertDate(data.get(0).getStartTime(),"yyyy-MM-dd HH:mm").getTime() > date.getTime())) {
                    viewHolder.tv.setBackgroundColor(Color.parseColor("#FB9F4A"));
                }
            }
        } else {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.textView.getLayoutParams();
            lp.topMargin = 90;
            viewHolder.textView.getLayoutParams();
            viewHolder.textView.setText("暂无会议");
        }


        return view;
    }

    private final class ViewHolder{
        TextView textView;
        TextView info;
        TextView tv;
        public ViewHolder(View view) {
            textView = (TextView) view.findViewById(R.id.name);
            info  = (TextView) view.findViewById(R.id.info);
            tv = (TextView) view.findViewById(R.id.tv);
        }
    }

    public MyAdapter(List<ConferenceReservationVO> data, Context context) {
        this.data = data;
        this.context = context;
    }
}

This is enough, in fact, it is only a few steps; of course, this is only a very elementary usage, because I am still learning. Well, the effect of starting is as follows:

 Because Xiaojie is also learning Android development, if you guys have a better method or find something wrong with me, please correct me in time! If there are friends who are also studying and have something unclear, you can also comment or private message to discuss together!

Guess you like

Origin blog.csdn.net/m0_64055755/article/details/127534164