之前写这个只是抱着半玩的心态,没有想到有这么多网友愿意驻足浏览,十分的惊喜。这里浅浅说一下我并不是专门学软件开发的,所以如果有什么错误请多指教。
接上回分解。现在我们来到第二个界面“感悟”:
fragment_login2.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fefefe"><TextViewandroid:id="@+id/note_name"android:layout_width="match_parent"android:layout_height="50dp"android:textSize="35dp"android:textColor="@android:color/white"android:gravity="center"android:textStyle="bold"android:text="感 悟"android:background="@drawable/bookbg"/><ListViewandroid:id="@+id/listview123"android:layout_width="match_parent"android:layout_height="match_parent"android:cacheColorHint="#00000000"android:divider="#E4E4E4"android:dividerHeight="1dp"android:fadingEdge="none"android:listSelector="#00000000"android:scrollbars="none"android:layout_below="@+id/note_name"android:background="@drawable/bookbg2"></ListView><ImageViewandroid:id="@+id/add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/add"android:layout_marginBottom="30dp"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:contentDescription="TODO" />
</RelativeLayout>
对应的java LoginFragment2 :
public class LoginFragment2 extends Fragment {ListView listView;List<NotepadBean> list;SQLiteHelper mSQLiteHelper;NotepadAdapter adapter;@Nullable@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_login2, container, false);listView = view.findViewById(R.id.listview123);ImageView add = view.findViewById(R.id.add);add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(getActivity(),RecordActivity.class);startActivityForResult(intent, 1);}});initData();return view;}protected void initData() {mSQLiteHelper = new SQLiteHelper(getActivity()); //创建数据库showQueryData();listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {NotepadBean notepadBean = list.get(position);Intent intent = new Intent(getActivity(), RecordActivity.class);intent.putExtra("id", notepadBean.getId());intent.putExtra("time", notepadBean.getNotepadTime()); //记录的时间intent.putExtra("content", notepadBean.getNotepadContent()); //记录的内容startActivityForResult(intent, 1);}});listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, final intposition, long id) {AlertDialog dialog;AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setMessage("是否删除此事件?").setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {NotepadBean notepadBean = list.get(position);if (mSQLiteHelper.deleteData(notepadBean.getId())) {list.remove(position);adapter.notifyDataSetChanged();Toast.makeText(getActivity(), "删除成功",Toast.LENGTH_SHORT).show();}}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});dialog = builder.create();dialog.show();return true;}});}private void showQueryData() {if (list != null) {list.clear();}//从数据库中查询数据(保存的标签)list = mSQLiteHelper.query();adapter = new NotepadAdapter(getActivity(), list);listView.setAdapter(adapter);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode == 1 && resultCode == 2) {showQueryData();}}}
然后我们点击添加按钮,也就是那个红红的加号,进去就是这个界面:
这里面可以添加任何你想写下的内容,比如我是梅西的球迷那么就可以写“阿根廷必胜!梅西必圆梦!”,如果你不满意左下角是删除,右边的是确认添加。
activity_record.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#fefefe"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="45dp"android:background="#fb7a6a"android:orientation="horizontal"><ImageViewandroid:id="@+id/note_back"android:layout_width="45dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:paddingLeft="11dp"android:src="@drawable/back" /><TextViewandroid:id="@+id/note_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:gravity="center"android:text="记事本"android:textColor="@android:color/white"android:textSize="35dp"android:textStyle="bold" /></RelativeLayout><TextViewandroid:id="@+id/tv_time"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="15sp"android:paddingTop="10dp"android:paddingBottom="10dp"android:gravity="center"android:visibility="gone"android:textColor="#689F38"/><EditTextandroid:id="@+id/note_content"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:gravity="top"android:hint="请输入要添加的内容"android:paddingLeft="5dp"android:textColor="@android:color/black"android:background="#fefefe" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#689F38"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="55dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/delete"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:src="@drawable/delete"android:paddingBottom="15dp"android:paddingTop="9dp"/><ImageViewandroid:id="@+id/note_save"android:layout_width="0dp"android:layout_weight="1"android:layout_height="wrap_content"android:src="@drawable/save_note"android:paddingBottom="15dp"android:paddingTop="9dp"/></LinearLayout>
</LinearLayout>
RecordActivity:
public class RecordActivity extends Activity implements View.OnClickListener {ImageView note_back;TextView note_time;EditText content;ImageView delete;ImageView note_save;SQLiteHelper mSQLiteHelper;TextView noteName;String id;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_record);note_back = (ImageView) findViewById(R.id.note_back);note_time = (TextView)findViewById(R.id.tv_time);content = (EditText) findViewById(R.id.note_content);delete = (ImageView) findViewById(R.id.delete);note_save = (ImageView) findViewById(R.id.note_save);noteName = (TextView) findViewById(R.id.note_name);note_back.setOnClickListener(this);delete.setOnClickListener(this);note_save.setOnClickListener(this);initData();}protected void initData() {mSQLiteHelper = new SQLiteHelper(this);noteName.setText("添加记录");Intent intent = getIntent();if(intent!= null){id = intent.getStringExtra("id");if (id != null){noteName.setText("修改记录");content.setText(intent.getStringExtra("content"));note_time.setText(intent.getStringExtra("time"));note_time.setVisibility(View.VISIBLE);}}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.note_back:finish();break;case R.id.delete:content.setText("");break;case R.id.note_save:String noteContent=content.getText().toString().trim();if (id != null){//修改操作if (noteContent.length()>0){if (mSQLiteHelper.updateData(id, noteContent, DBUtils.getTime())){showToast("修改成功");setResult(2);finish();}else {showToast("修改失败");}}else {showToast("修改内容不能为空!");}}else {//向数据库中添加数据if (noteContent.length()>0){if (mSQLiteHelper.insertData(noteContent, DBUtils.getTime())){showToast("保存成功");setResult(2);finish();}else {showToast("保存失败");}}else {showToast("修改内容不能为空!");}}break;}}public void showToast(String message){Toast.makeText(RecordActivity.this,message,Toast.LENGTH_SHORT).show();}
}
保存之后我们会返回感悟的界面现在就会有我们之前写下的记录,长按是可以删除的:
以上的这些是要依托数据库的构建才能实现的,文件有三:
构建个适配器NotepadAdapter:
public class NotepadAdapter extends BaseAdapter {private LayoutInflater layoutInflater;private List<NotepadBean> list;public NotepadAdapter(Context context, List<NotepadBean> list){this.layoutInflater=LayoutInflater.from(context);this.list=list;}@Overridepublic int getCount() {return list==null ? 0 : list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView==null){convertView=layoutInflater.inflate(R.layout.notepad_item_layout,null);viewHolder=new ViewHolder(convertView);convertView.setTag(viewHolder);}else {viewHolder=(ViewHolder) convertView.getTag();}NotepadBean noteInfo=(NotepadBean) getItem(position);viewHolder.tvNoteoadContent.setText(noteInfo.getNotepadContent());viewHolder.tvNotepadTime.setText(noteInfo.getNotepadTime());return convertView;}class ViewHolder{TextView tvNoteoadContent;;TextView tvNotepadTime;public ViewHolder(View view){tvNoteoadContent=(TextView) view.findViewById(R.id.item_content);tvNotepadTime=(TextView) view.findViewById(R.id.item_time);}}
}
NotepadBean:
public class NotepadBean {private String id; //记录的idprivate String notepadContent; //记录的内容private String notepadTime; //保存记录的时间public String getId() {return id;}public void setId(String id) {this.id = id;}public String getNotepadContent() {return notepadContent;}public void setNotepadContent(String notepadContent) {this.notepadContent = notepadContent;}public String getNotepadTime() {return notepadTime;}public void setNotepadTime(String notepadTime) {this.notepadTime = notepadTime;}
}
这个是重中之重,实现了数据库的增删改查
SQLiteHelper:
public class SQLiteHelper extends SQLiteOpenHelper {private SQLiteDatabase sqLiteDatabase;//创建数据库public SQLiteHelper(Context context){super(context, DBUtils.DATABASE_NAME, null, DBUtils.DATABASE_VERION);sqLiteDatabase = this.getWritableDatabase();}//创建表@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table "+DBUtils.DATABASE_TABLE+"("+DBUtils.NOTEPAD_ID+" integer primary key autoincrement,"+ DBUtils.NOTEPAD_CONTENT +" text," + DBUtils.NOTEPAD_TIME+ " text)");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}//添加数据public boolean insertData(String userContent,String userTime){ContentValues contentValues=new ContentValues();contentValues.put(DBUtils.NOTEPAD_CONTENT,userContent);contentValues.put(DBUtils.NOTEPAD_TIME,userTime);returnsqLiteDatabase.insert(DBUtils.DATABASE_TABLE,null,contentValues)>0;}//删除数据public boolean deleteData(String id){String sql=DBUtils.NOTEPAD_ID+"=?";String[] contentValuesArray=new String[]{String.valueOf(id)};returnsqLiteDatabase.delete(DBUtils.DATABASE_TABLE,sql,contentValuesArray)>0;}//修改数据public boolean updateData(String id,String content,String userYear){ContentValues contentValues=new ContentValues();contentValues.put(DBUtils.NOTEPAD_CONTENT,content);contentValues.put(DBUtils.NOTEPAD_TIME,userYear);String sql=DBUtils.NOTEPAD_ID+"=?";String[] strings=new String[]{id};returnsqLiteDatabase.update(DBUtils.DATABASE_TABLE,contentValues,sql,strings)>0;}//查询数据public List<NotepadBean> query(){List<NotepadBean> list=new ArrayList<NotepadBean>();Cursor cursor=sqLiteDatabase.query(DBUtils.DATABASE_TABLE,null,null,null,null,null,DBUtils.NOTEPAD_ID+" desc");if (cursor!=null){while (cursor.moveToNext()){NotepadBean noteInfo=new NotepadBean();@SuppressLint("Range") String id = String.valueOf(cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID)));@SuppressLint("Range") String content = cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT));@SuppressLint("Range") String time = cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_TIME));noteInfo.setId(id);noteInfo.setNotepadContent(content);noteInfo.setNotepadTime(time);list.add(noteInfo);}cursor.close();}return list;}
}
第三部分是一个分类,按照当初我的设想是与书架联系起来,但是肝到后面就累了,就简简单单实现一个分类,有亿丢丢粗糙,别介意哈。
fragment_login3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".LoginFragment3"><LinearLayoutandroid:layout_weight="1"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/lg3_textView3"android:layout_width="match_parent"android:layout_height="50dp"android:text="分 类"android:gravity="center"android:textSize="35dp"android:textColor="@color/white"android:background="@drawable/bookbg"></TextView><ListViewandroid:id="@+id/lg3_listView"android:textSize="35dp"android:gravity="center"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bookbg2"/></LinearLayout></LinearLayout>
LoginFragment3:
public class LoginFragment3 extends Fragment implements AdapterView.OnItemClickListener{
ListView listView;
SimpleAdapter simpleAdapter;public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {View view = inflater.inflate(R.layout.fragment_login3, container, false);listView = view.findViewById(R.id.lg3_listView);simpleAdapter = new SimpleAdapter(getActivity(),getData(),R.layout.item3,new String[]{"title"},new int[]{R.id.item_tv});listView.setAdapter(simpleAdapter);listView.setOnItemClickListener(this);return view;}private List<Map<String,Object>> getData() {String [] titles={"玄幻","都市","科幻","历史","添加书籍"};List<Map<String,Object>> list= new ArrayList<>();for(int i=0;i<5;i++){Map map = new HashMap();map.put("title",titles[i]);list.add(map);}return list;}public void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {switch(position){case 0:Intent intent1=new Intent(getActivity(),jiemian1.class);startActivity(intent1);break;case 1:Intent intent2=new Intent(getActivity(),jiemian2.class);startActivity(intent2);break;case 2:Intent intent3=new Intent(getActivity(),jiemian3.class);startActivity(intent3);break;case 3:Intent intent4=new Intent(getActivity(),jiemian4.class);startActivity(intent4);break;case 4:Intent intent5=new Intent(getActivity(),jiemian5.class);startActivity(intent5);break;default:throw new IllegalStateException("Unexpected value: " + position);}}
}
点击添加:
activity_jiemian5.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"android:background="@drawable/bookbg2"><TextViewandroid:id="@+id/lg3_textView3"android:layout_width="match_parent"android:layout_height="50dp"android:text="添加书籍"android:gravity="center"android:textSize="35dp"android:textColor="@color/white"android:background="@drawable/bookbg"></TextView><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLines="1"android:text="书籍名"></TextView><EditTextandroid:id="@+id/bookname"android:layout_width="match_parent"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLines="1"android:text="类型"></TextView><EditTextandroid:id="@+id/sort1"android:layout_width="match_parent"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLines="1"android:text="作者"></TextView><EditTextandroid:id="@+id/author"android:layout_width="match_parent"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="100dp"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLines="1"android:text="简介"></TextView><EditTextandroid:id="@+id/short1"android:layout_width="match_parent"android:layout_height="wrap_content"></EditText></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="100dp"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:maxLines="1"android:text="内容"></TextView><EditTextandroid:id="@+id/judge"android:layout_width="match_parent"android:layout_height="30dp"></EditText></LinearLayout><Buttonandroid:onClick="insert"android:layout_width="200dp"android:layout_height="50dp"android:text="上传书籍"android:layout_gravity="center"></Button></LinearLayout>
jiemian5 :
public class jiemian5 extends AppCompatActivity {private EditText bookname,sort1,author,short1,judge;private MySQLiteOpenHelper mySQLiteOpenHelper;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_jiemian5);initview();mySQLiteOpenHelper = new MySQLiteOpenHelper(this);}private void initview() {bookname = findViewById(R.id.bookname);sort1 = findViewById(R.id.sort1);author = findViewById(R.id.author);short1 = findViewById(R.id.short1);judge = findViewById(R.id.judge);}public void insert(View view) {String Ebookname = bookname.getText().toString().trim();String Esort1 = sort1.getText().toString().trim();String Eauthor = author.getText().toString().trim();String Eshort1 = short1.getText().toString().trim();String Ejudge = judge.getText().toString().trim();Ebook ebook1 = new Ebook();ebook1.setBookname(Ebookname);ebook1.setAuthor(Eauthor);ebook1.setSort1(Esort1);ebook1.setEshort1(Eshort1);ebook1.setEjudge(Ejudge);long rowid = mySQLiteOpenHelper.insertbook(ebook1);if (rowid != -1){Toast.makeText(this,"添加成功!",Toast.LENGTH_SHORT).show();}else {Toast.makeText(this,"添加失败!",Toast.LENGTH_SHORT).show();}}
}
因为不能实现与前面的联动,那只好自己输入数据咯,输入完后看看效果:
说明下我是不看网络小说的,以上内容是某度推荐的,如有错误还请海涵。
这四个内容差不多,我以第一个举例:
activity_jiemian1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".jiemian1"android:background="@drawable/bookbg2"><TextViewandroid:id="@+id/lg3_textView3"android:layout_width="match_parent"android:layout_height="50dp"android:text="玄幻"android:gravity="center"android:textSize="35dp"android:textColor="@color/white"android:background="@drawable/bookbg"></TextView><ListViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/listview1"android:background="@drawable/bookbg2"></ListView>
</LinearLayout>
jiemian1:
public class jiemian1 extends AppCompatActivity {private ListView listView;private MySQLiteOpenHelper mySQLiteOpenHelper;private Cursor cursor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_jiemian1);mySQLiteOpenHelper = new MySQLiteOpenHelper(this);listView = findViewById(R.id.listview1);final SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.list_item, new String[]{"a", "b", "c"}, new int[]{R.id.booknamex, R.id.authorx, R.id.shortx} );listView.setAdapter(adapter);}private List<Map<String,Object>> getData() {SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();String Eguanjianzi = "玄幻";List<Map<String,Object>> list = new ArrayList<>();cursor = db.query("Ebook008", null, "sort like ?", new String[]{Eguanjianzi}, null, null, null);while (cursor.moveToNext()) {String bookname = cursor.getString(1);String author = cursor.getString(3);String short1 = cursor.getString(4);Map map = new HashMap();map.put("a", bookname);map.put("b", author);map.put("c", short1);list.add(map);}cursor.close();return list;}}
然后仍是一个数据库:
MySQLiteOpenHelper
public class MySQLiteOpenHelper extends SQLiteOpenHelper {public MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);}private static final String DB_NAME = "mysqlite008.db";private static final String TABLE_NAME_EBOOK = "Ebook008";private static final String create_table_sql = "create table " + TABLE_NAME_EBOOK + "(id integer primary key autoincrement, bookname text ,sort text ,author text,short text,judge text)";public MySQLiteOpenHelper(Context context) {super(context, DB_NAME, null, 1);}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL(create_table_sql);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}public long insertbook(Ebook ebook) {SQLiteDatabase db = getWritableDatabase();ContentValues values = new ContentValues();values.put("bookname", ebook.getBookname());values.put("sort", ebook.getSort1());values.put("author", ebook.getAuthor());values.put("short", ebook.getEshort1());values.put("judge", ebook.getEjudge());return db.insert(TABLE_NAME_EBOOK, null, values);}public List<Ebook> chaxun(String guanjianzi){List<Ebook> ebooks = new ArrayList<>();SQLiteDatabase db = getWritableDatabase();Cursor cursor = db.query(TABLE_NAME_EBOOK,null,"sort like ?",new String[]{guanjianzi},null,null,null);if (cursor!= null){while (cursor.moveToNext()){String bookname = cursor.getString(1);String sort1 = cursor.getString(2);String author = cursor.getString(3);String short1 = cursor.getString(4);String judge =cursor.getString(5);Ebook ebook1 = new Ebook();ebook1.setBookname(bookname);ebook1.setSort1(sort1);ebook1.setAuthor(author);ebook1.setEshort1(short1);ebook1.setEjudge(judge);ebooks.add(ebook1);}
// cursor.close();}return ebooks;}
}
最后有几个小的xml布局文件可能前面没给到,本来想这次直接给的,但是不知道是这次编写的太长还是什么的,我现在打字都很卡,人麻了,就先告退了。