Android开发_记事本(3)
适配器NoteAdapter
相当于数据和ListView之间的中介
package com.example.note;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
//BaseAdapter:几乎每一个适配器都会继承的superclass,Filterable方便对内容进行排序
public class NoteAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<Note> backList; //用来备份原始数据,防止更新失败时丢失之前的数据
private List<Note> noteList; //这个数据是会改变的,所以要有个变量来备份一下原始数据
private MyFilter myFilter;
public NoteAdapter(Context mContext,List<Note> notelist) {
this.mContext = mContext;
this.noteList = notelist;
backList = noteList;
}
@Override
public int getCount(){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 converView, ViewGroup parent){
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mContext.setTheme((sharedPreferences.getBoolean("nightMode",false)? R.style.NightTheme:R.style.DayTheme));
View v = View.inflate(mContext,R.layout.note_layout,null); //渲染一个View
TextView tv_content = (TextView)v.findViewById(R.id.tv_content); //第一行内容
TextView tv_time = (TextView)v.findViewById(R.id.tv_time); //第二行时间
//set text for TextView
String allText = noteList.get(position).getContent(); //获取每一个content
/*if(sharedPreferences.getBoolean("noteTitle",true)){
tv_content.setText(allText.split("\n")[0]);
} */
tv_content.setText(allText); //进行赋值
tv_time.setText(noteList.get(position).getTime());
//save note id to tag
v.setTag(noteList.get(position).getId());
return v;
}
@Override
public Filter getFilter(){
if(myFilter == null){
myFilter = new MyFilter();
}
return myFilter;
}
class MyFilter extends Filter{
//我们在perfoFiltering(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; //将得到的集合保存到FilterResult的val变量中
result.count = list.size(); //将集合的大小保存到FilterResult的count变量中
return result;
}
//在publishResult方法中告诉适配器更新界面
@Override
protected void publishResults(CharSequence charSequence,FilterResults filterResults){
noteList = (List<Note>)filterResults.values;
if(filterResults.count > 0){
notifyDataSetChanged(); //通知数据发生了改变
}else{
notifyDataSetChanged(); //通知数据失效
}
}
}
}
note_layout
以某种布局排列ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/note_shape"
android:orientation="vertical"
android:outlineAmbientShadowColor="@color/black"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="12dp">
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="Content"
android:textColor="?attr/tvMainColor"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textColor="@color/greyC"
android:textSize="16dp" />
</LinearLayout>
效果:
此时的MainActivity
package com.example.note;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends BaceActivity {
private NoteDatabase dbHelper;
private Context context = this;
final String TAG = "tag";
FloatingActionButton btn; //后面再初始化
TextView tv;
//笔记本的核心:ListView
private ListView lv;
private NoteAdapter adapter;
private List<Note> noteList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//floatingbutton初始化
btn = ( FloatingActionButton)findViewById(R.id.fab);
lv = findViewById(R.id.lv);
adapter = new NoteAdapter(getApplicationContext(),noteList); //获取app的contex的function
refreshListView(); //每次更新的时候需要刷新listview
lv.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//测试btn
//Log.d(TAG,"onClick:click");
Intent intent = new Intent(MainActivity.this,EditActivity.class);//从参数一跳转到参数二
//启动活动并获取结果
startActivityForResult(intent,0); //启动参数这样的一个跳转
}
});
}
//下面接受startActivityForResult结果
@Override
//通过上面的Intent跳转到Edit,并通过下面的函数来对Edit的内容进行接收
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
String content = data.getStringExtra("content");//获取edit的content值和time值
String time = data.getStringExtra("time");
Note note = new Note(content,time,1);
//调用CRUD
CRUD op = new CRUD(context);
op.open();
op.addNote(note);
op.close();
refreshListView(); //更改完结果后就刷新,没有这条语句则会在下一次打开app时刷新
}
public void refreshListView(){
CRUD op = new CRUD(context);
op.open();
//set adapter
if(noteList.size() > 0){ //如果当前的list非空,则清楚所有的,再加上更新后所有的
noteList.clear();
}
noteList.addAll(op.getAllNotes());
op.close();
adapter.notifyDataSetChanged();
}
}
此时实现效果
写完笔记返回时可以看到
但无法点击、修改list内容等进一步操作