Androidstudio的简易记事本Android studio 的简易记事本
最近学习做了⼀个安卓的简易记事本,脑海中有很多可以拓展的地⽅,先把简单的记录⼀下。具体样式如下图
然后就是具体代码了
⼀共创建了四个类,分别是Adpter类,bean类,database类,util类⾸先是Adpter类
Adpter类
NotepadAdapter.java
ample.chapter_07_211.adapter;
t.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
ample.chapter_07_211.R;
ample.chapter_07_211.bean.NotepadBean;
import java.util.List;
public class NotepadAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<NotepadBean> list;
public NotepadAdapter(Context context, List<NotepadBean> list){
layoutInflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount(){
return list==null?0:list.size();
}
@Override
public Object getItem(int position){
(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(pad_item_layout,null);            viewHolder =new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
}else{
viewHolder =(ViewHolder) Tag();
}
NotepadBean notepadBean =(NotepadBean)getItem(position);
viewHolder.tvNotepadContent.NotepadContent());        viewHolder.tvNotepadTime.NotepadTime());
return convertView;
}
class ViewHolder{
TextView tvNotepadContent;
TextView tvNotepadTime;
public ViewHolder(View view){
tvNotepadContent =  view.findViewById(R.id.item_content);//记录的内容          tvNotepadTime = view.findViewById(R.id.item_time);//记录的时间
}
}
}
然后是bean类
ample.chapter_07_211.bean;
public class NotepadBean {
private String id;
private String notepadContent;//记录的内容
private String notepadTime;//记录的时间
public String getId(){
return id;
}
public String getNotepadContent(){
return notepadContent;
}
public void setNotepadContent(String notepadContent){
}
public String getNotepadTime(){
return notepadTime;
}
public void setNotepadTime(String notepadTime){
}
public void setId(String id){
this.id = id;
}
}
再然后是database类
database类
SQLiteHelper.java
ample.chapter_07_211.adapter;
t.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
ample.chapter_07_211.R;
ample.chapter_07_211.bean.NotepadBean;
import java.util.List;
public class NotepadAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<NotepadBean> list;
public NotepadAdapter(Context context, List<NotepadBean> list){
layoutInflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public int getCount(){
return list==null?0:list.size();
}
@Override
public Object getItem(int position){
(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if(convertView == null){
convertView = layoutInflater.inflate(pad_item_layout,null);            viewHolder =new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else{
viewHolder =(ViewHolder) Tag();
}
NotepadBean notepadBean =(NotepadBean)getItem(position);
viewHolder.tvNotepadContent.NotepadContent());        viewHolder.tvNotepadTime.NotepadTime());
return convertView;
}
class ViewHolder{
TextView tvNotepadContent;
TextView tvNotepadTime;
public ViewHolder(View view){
tvNotepadContent =  view.findViewById(R.id.item_content);//记录的内容          tvNotepadTime = view.findViewById(R.id.item_time);//记录的时间
}
}
}
再再然后是util类
util类
DBUtils.java
ample.chapter_07_211.utils;
SimpleDateFormat;
import java.util.Date;
public class DBUtils {
public static final String DATABASE_NAME ="Notepad";//数据库名
public static final String DATABASE_TABLE ="Note";//表名
public static final int DATABASE_VERION =1;//数据库版本
//数据库表中的列名
public static final String NOTEPAD_ID ="id";
public static final String NOTEPAD_CONTENT ="content";
public static final String NOTEPAD_TIME ="notetime";
//获取当前⽇期
public static final String getTime(){
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM⽉dd⽇ HH:mm:ss");        Date date =new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
}
再再再然后就是我们的主类MainActivity了
MainActivity.javanotepad++
ample.chapter_07_211;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
t.DialogInterface;
t.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
ample.chapter_07_211.adapter.NotepadAdapter;
ample.chapter_07_211.bean.NotepadBean;
ample.chapter_07_211.database.SQLiteHelper;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private SQLiteHelper mSQLiteHelper;
private List<NotepadBean> list;
NotepadAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
listView =findViewById(R.id.List_view);
ImageView imageView =findViewById(R.id.add);
initData();
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。