NoteActivity

本文介绍了一个Android NoteAdapter类,它继承自BaseAdapter并实现了Filterable接口。该适配器用于管理动态过滤和显示Note对象列表,支持夜间模式切换。关键在于MyFilter类,通过自定义过滤规则高效筛选内容。

public class NoteAdapter extends BaseAdapter implements Filterable {
    private Context mcontext;
    private List<Note> backList;//用来备份原始数据
    private List<Note>noteList;//这个数据是会改变的,所以要有个变量来备份一下原始数据
    private MyFilter mFilter;
    public NoteAdapter(Context context,List<Note> noteList){
        Log.d("Adapter","adapter_creat");
        this.mcontext=context;
        this.noteList=noteList;
        backList=noteList;
    }

    @Override
    public int getCount() {
        Log.d("Adapter","adapter_noteList.size");
        return noteList.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d("Adapter","adapter_view");
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mcontext);
        mcontext.setTheme((sharedPreferences.getBoolean("nightMode", false)? R.style.NightTheme: R.style.DayTheme));
        @SuppressLint("ViewHolder") View v=View.inflate(mcontext,R.layout.note_layout,null);
        TextView tv_content=v.findViewById(R.id.tv_content);
        TextView tv_time=v.findViewById(R.id.tv_time);

        String allText=noteList.get(position).getContent();
        tv_content.setText(allText);
        tv_time.setText(noteList.get(position).getTime());
        v.setTag(noteList.get(position).getId());
        return v;
    }

    @Override
    public Filter getFilter() {
        if (mFilter==null){
            mFilter=new MyFilter();
        }
        return mFilter;
    }

    class MyFilter extends Filter {
        //我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults result = new FilterResults();
            List<Note> list;
            if (TextUtils.isEmpty(charSequence)) {//当过滤的关键字为空的时候,我们则显示所有的数据
                list = backList;
            } else {//否则把符合条件的数据对象添加到集合中
                list = new ArrayList<>();
                for (Note note : backList) {
                    if (note.getContent().contains(charSequence)) {
                        list.add(note);
                    }

                }
            }
            result.values = list; //将得到的集合保存到FilterResults的value变量中
            result.count = list.size();//将集合的大小保存到FilterResults的count变量中

            return result;
        }
        //在publishResults方法中告诉适配器更新界面
        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            noteList = (List<Note>)filterResults.values;
            if (filterResults.count>0){
                notifyDataSetChanged();//通知数据发生了改变
            }else {
                notifyDataSetInvalidated();//通知数据失效
            }
        }
    }

}

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeActivity"> <TextView android:id="@+id/tv_user" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> package com.lss.loginregister; import android.os.Bundle; import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); setContentView(R.layout.activity_home); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); // 获取 Intent 中传递的数据 int userId = getIntent().getIntExtra("user_id", -1); // 默认值为 -1 String username = getIntent().getStringExtra("username"); String password = getIntent().getStringExtra("password"); String phone = getIntent().getStringExtra("phone"); String email = getIntent().getStringExtra("email"); TextView tvUser = findViewById(R.id.tv_user); String user = "账户:" + username + "\n" + "密码:" + password + "\n" + "手机:" + phone + "\n" + "邮箱:" + email; tvUser.setText(user); } } ------ 要求对这个home页进行修改,采用material提供组件,在home页可以显示DataTransmissionActivity、JsonParseActivity和NoteActivity
07-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值