每日总结2023/3/4

今天学习了数据库在界面的显示

通过ListView显示数据库信息

 

 创建xml文件,作为ListView中的item

创建适配器

package com.example.meiri.db;
/*
*listview适配器
* */
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.meiri.R;
import com.example.meiri.bean.Punch;

import java.util.List;

public class PunchBaseAdapter extends BaseAdapter {
    Context context;
    List<Punch> mdatas;
    LayoutInflater inflater;

    public PunchBaseAdapter(Context context, List<Punch> mdatas) {
        this.context = context;
        this.mdatas = mdatas;
        this.inflater = inflater;
    }

    @Override
    public int getCount() {
        return mdatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mdatas.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHonder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_mainlv, parent, false);
            holder = new ViewHonder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHonder) convertView.getTag();
        }
        Punch punch = mdatas.get(position);
        holder.tv_key.setText(punch.getKeys());
        holder.tv_text.setText(punch.getSummarize());
        holder.tv_date.setText(punch.getDate());
        holder.tv_day.setText(punch.getDay());
        return convertView;
    }

    class ViewHonder {
        TextView tv_key, tv_text, tv_date, tv_day;     //  关键字,内容,时间,坚持天数

        public ViewHonder(View view) {
            tv_key = view.findViewById(R.id.item_mainlv_keys);
            tv_text = view.findViewById(R.id.item_mainlv_tv_text);
            tv_date = view.findViewById(R.id.item_mainlv_date);
            tv_day = view.findViewById(R.id.item_mainlv_day);
        }

    }
}

 

数据库中查询所有

public static ArrayList<Punch> get_punch_data() {
        ArrayList<Punch> list = new ArrayList<>();
        Cursor cursor = pb.query("userpunch", null, null, null, null, null, "date DESC");
        while (cursor.moveToNext()) {
            int index_date = cursor.getColumnIndex("date");
            int index_keys = cursor.getColumnIndex("keys");
            int index_summarize = cursor.getColumnIndex("summarize");
            int index_day = cursor.getColumnIndex("day");
            int index_maxday = cursor.getColumnIndex("maxday");
            String date = cursor.getString(index_date);
            String keys = cursor.getString(index_keys);
            String summarize = cursor.getString(index_summarize);
            String day = cursor.getString(index_day);
            String maxday = cursor.getString(index_maxday);
            list.add(new Punch(date,keys,summarize,day,maxday));
        }
        return list;
    }

 

 

界面对应的java文件中

    private ArrayList<Punch> punchList;   //声明数据源
    private PunchBaseAdapter adapter;       //适配器

 

在启动时添加

        ListView lv = findViewById(R.id.main_lv);
        punchList = new ArrayList<>();

        adapter = new PunchBaseAdapter(MainActivity.this, punchList);
        lv.setAdapter(adapter);

 

新添加内容返回显示需要

    @Override
    protected void onResume() {
        super.onResume();
        local_Data();
    }

    private void local_Data() {
        ArrayList<Punch> punchArrayList = PunchHelper.get_punch_data();
        punchList.clear();
        punchList.addAll(punchArrayList);
        adapter.notifyDataSetChanged();
    }

 

posted @ 2023-03-04 18:47  花伤错零  阅读(19)  评论(0)    收藏  举报