安卓实训开发-笔记本之主界面

ZAESKY VIP4 2020-1-27

969 0

接着上个帖子的登录注册功能完成后,我们接着做登录之后的主界面功能。

里面还包含了一个很低级的数据备份恢复功能,太低级就不单独拿出来做展示。


主界面实现代码

package com.example.notebook;
import java.util.ArrayList;
import java.util.List;
import android.R.color;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class NoteActivity extends Activity {
private Button addbuttton,ckButton,xgButton,cxButton,scButton,bfButton,gyButton;
private ListView noteListView;
private List<Notelist> notelist= new ArrayList<Notelist>();
private Myadapter myAdapter=null;
private int current = 0;
        protected void onCreate(Bundle savedInstanceState) {
                 // TODO Auto-generated method stub
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.activity_listview);
                 /*----------------------------------主页显示功能------------------------------------*/
                 MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(NoteActivity.this);
                 mydatabaseHelper.getWritableDatabase();
                 Cursor result=mydatabaseHelper.findnoteCursor();
                 Toast.makeText(NoteActivity.this,"共查询到"+String.valueOf(result.getCount())+"条数据", Toast.LENGTH_LONG).show();
                 noteListView=(ListView)findViewById(R.id.lv_note);
                 //循环取出数据
                 for (result.moveToFirst();!result.isAfterLast();result.moveToNext()) {
                         Notelist notelists= new Notelist(result.getInt(0),result.getString(1),result.getString(2),result.getString(3));
                         notelist.add(notelists);
                 }
                 //绑定适配器
                 myAdapter = new Myadapter();
                 noteListView.setAdapter(myAdapter);
                 //listview项目单击事件监听
                 noteListView.setOnItemClickListener(new OnItemClickListener() {
                         @Override
                         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                          long arg3) {
                                  // TODO Auto-generated method stub
                                  current=arg2;
                                  myAdapter.notifyDataSetChanged();
                         }
                 });
                 /*----------------------------------备份与恢复图形按钮------------------------------------*/
                 bfButton=(Button)findViewById(R.id.listview_backup);
                 bfButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View arg0) {
                                  // 对话框形式
                                  Dialog dialog=new AlertDialog.Builder(NoteActivity.this)
                                                  .setTitle("备份与恢复")
                                                  .setMessage("你是要备份还是恢复?")
                                                  .setPositiveButton("备份", new DialogInterface.OnClickListener() {
                                                           public void onClick(DialogInterface dialog, int which) {
                                                                   // TODO Auto-generated method stub
                                                                   int currentid=notelist.get(current).getId();
                                                                   MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(NoteActivity.this);
                                                                   mydatabaseHelper.backupdelete();
                                                                   mydatabaseHelper.backup();
                                                                   Toast.makeText(NoteActivity.this, "备份成功!", Toast.LENGTH_LONG).show();
                                                           }
                                                  })
                                                  .setNegativeButton("恢复", new DialogInterface.OnClickListener() {
                                                           @Override
                                                           public void onClick(DialogInterface dialog, int which) {
                                                                   // TODO Auto-generated method stub
                                                                   MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(NoteActivity.this);
                                                                   mydatabaseHelper.recoverdelete();
                                                                   mydatabaseHelper.recover();
                                                                   getallnote();
                                                                   myAdapter.notifyDataSetChanged();
                                                                   Toast.makeText(NoteActivity.this, "恢复成功!", Toast.LENGTH_LONG).show();
                                                           }
                                                  })
                                                  .create();
                 dialog.show();
                         }
                 });
                 /*----------------------------------查看图形按钮------------------------------------*/
                 ckButton=(Button)findViewById(R.id.listview_cheak);
                 ckButton.setOnClickListener(new OnClickListener() {
                         @Override
                         public void onClick(View v) {
                                  // Intent邮差,给Activity传递数据
                                  Intent searchIntent =new Intent(NoteActivity.this,CheakActivity.class);
                                  //将当前用户所选项数据传递
                                  Notelist cheakNotelist=new Notelist();
                                  //获取当前所选id数据赋值给cheakNotelist
                                 cheakNotelist=notelist.get(current);
                                  //创建一个Bundle对象
                                  Bundle cheakBundle=new Bundle();
                                  //将数据存储到cheakBundle中,调用Serializable方法
                                  cheakBundle.putSerializable("note", cheakNotelist);
                                  //将Bundle对象传入intent中
                                  searchIntent.putExtras(cheakBundle);
                                  //启动intent
                                  startActivity(searchIntent);
                         }
                 });
                 /*----------------------------------修改图形按钮------------------------------------*/
                 xgButton=(Button)findViewById(R.id.listview_xiugai);
                 xgButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  Intent intent =new Intent(NoteActivity.this,EditActivity.class);
                                  Notelist currentnotelist=new Notelist();
                                  currentnotelist=notelist.get(current);
                                  Bundle bundle=new Bundle();
                                  bundle.putSerializable("note", currentnotelist);
                                  intent.putExtras(bundle);
                                  startActivity(intent);
                         }
                 });
                 /*----------------------------------查询图形按钮------------------------------------*/
                 cxButton=(Button)findViewById(R.id.listview_chaxun);
                 cxButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  Intent intent=new Intent(NoteActivity.this,SearchActivity.class);
                                  startActivity(intent);
                         }
                 });
                 /*----------------------------------删除图形按钮------------------------------------*/
                 scButton=(Button)findViewById(R.id.listview_shanchu);
                scButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  Dialog dialog=new AlertDialog.Builder(NoteActivity.this)
                                                  .setTitle("删除")
                                                  .setMessage("你确定要删除这条记事吗?")
                                                  .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                           public void onClick(DialogInterface dialog, int which) {
                                                                   // TODO Auto-generated method stub
                                                                   int currentid=notelist.get(current).getId();
                                                                   MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(NoteActivity.this);
                                                                   mydatabaseHelper.eventdelete(currentid);
                                                                   Toast.makeText(NoteActivity.this, "删除成功!", Toast.LENGTH_LONG).show();
                                                                   getallnote();
                                                                   myAdapter.notifyDataSetChanged();
                                                           }
                                                  })
                                                  .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                           public void onClick(DialogInterface dialog, int which) {
                                                           }
                                                  })
                                                  .create();
                 dialog.show();
                         }
                 });
                 /*----------------------------------添加图形按钮------------------------------------*/
                 addbuttton=(Button)findViewById(R.id.listview_btnadd);
                 addbuttton.setOnClickListener(new OnClickListener() {
                         public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  Intent intent=new Intent(NoteActivity.this,AddnoteActivity.class);
                                  startActivity(intent);
                         }
                 });
                 /*----------------------------------关于图形按钮------------------------------------*/
                 gyButton=(Button)findViewById(R.id.listview_about);
                 gyButton.setOnClickListener(new OnClickListener() {
                         public void onClick(View v) {
                                  // TODO Auto-generated method stub
                                  Intent intent=new Intent(NoteActivity.this,AboutActivity.class);
                                  startActivity(intent);
                         }
                 });
        }
        /*----------------------------------系统菜单功能------------------------------------*/
        public boolean onCreateOptionsMenu(Menu menu) {
                 // TODO Auto-generated method stub
                 menu.add(0,1,1,"添加");
                 menu.add(0,2,2,"修改");
                 menu.add(0,3,3,"删除");
                 menu.add(0,4,4,"查询");
                 menu.add(0,5,5,"查看");
                 return super.onCreateOptionsMenu(menu);
        }
        /*----------------------------------系统菜单单击事件监听------------------------------------*/
        public boolean onOptionsItemSelected(MenuItem item) {
                 // 菜单选项
                 switch (item.getItemId()) {
                 //添加功能
                 case 1:
                         Intent intent=new Intent(NoteActivity.this,AddnoteActivity.class);
                         startActivity(intent);
                         break;
                 //修改功能
                 case 2:
                         intent =new Intent(NoteActivity.this,EditActivity.class);
                         Notelist currentnotelist=new Notelist();
                         currentnotelist=notelist.get(current);
                         Bundle bundle=new Bundle();
                         bundle.putSerializable("note", currentnotelist);
                         intent.putExtras(bundle);
                         startActivity(intent);
                         break;
                 //删除功能
                 case 3:
                         Dialog dialog=new AlertDialog.Builder(NoteActivity.this)
                                                           .setTitle("删除")
                                                          .setMessage("你确定要删除这条记事吗?")
                                                           .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                                   public void onClick(DialogInterface dialog, int which) {
                                                                            // TODO Auto-generated method stub
                                                                            int currentid=notelist.get(current).getId();
                                                                            MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(NoteActivity.this);
                                                                            mydatabaseHelper.eventdelete(currentid);
                                                                           Toast.makeText(NoteActivity.this, "删除成功!", Toast.LENGTH_LONG).show();
                                                                            getallnote();
                                                                            myAdapter.notifyDataSetChanged();
                                                                   }
                                                          })
                                                           .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                                   public void onClick(DialogInterface dialog, int which) {
                                                                   }
                                                           })
                                                           .create();
                         dialog.show();
                                          break;      
                 //查询功能
                 case 4:
                         Intent searchIntent=new Intent(NoteActivity.this,SearchActivity.class);
                         startActivity(searchIntent);
                         break;
                 //查看功能
                 case 5:
                         searchIntent =new Intent(NoteActivity.this,CheakActivity.class);
                         Notelist cheakNotelist=new Notelist();
                         cheakNotelist=notelist.get(current);
                         Bundle cheakBundle=new Bundle();
                         cheakBundle.putSerializable("note", cheakNotelist);
                         searchIntent.putExtras(cheakBundle);
                         startActivity(searchIntent);
                         break;
                default:
                         break;
                 }
                 return super.onOptionsItemSelected(item);
        }
        /*----------------------------------用于刷新重新读取数据方法------------------------------------*/
        public void getallnote() {
                 MydatabaseHelper mydatabaseHelper=new MydatabaseHelper(NoteActivity.this);
                 Cursor noteCursor=mydatabaseHelper.getAllnote();
                 notelist.clear();
                 for (noteCursor.moveToFirst();!noteCursor.isAfterLast();noteCursor.moveToNext()) {
                         Notelist notelists=new Notelist();
                         notelists.setId(noteCursor.getInt(0));
                         notelists.setTitleString(noteCursor.getString(1));
                         notelists.setEventString(noteCursor.getString(2));
                         notelists.setDateString(noteCursor.getString(3));
                         notelist.add(notelists);
                 }
        }
        /*----------------------------------重启刷新------------------------------------*/
        protected void onRestart() {
                 super.onRestart();
                 getallnote();
                 myAdapter.notifyDataSetChanged();
        }
        /*----------------------------------自定义适配器类------------------------------------*/
        class Myadapter extends BaseAdapter{
                 public int getCount() {
                         // TODO Auto-generated method stub
                         return notelist.size();
                 }
                 public Object getItem(int arg0) {
                         // TODO Auto-generated method stub
                         return notelist.get(arg0);
                 }
                 public long getItemId(int arg0) {
                         // TODO Auto-generated method stub
                         return arg0;
                 }
                 public View getView(int arg0, View arg1, ViewGroup arg2) {
                         // TODO Auto-generated method stub
                         View myView=null;
                         myView=arg1.inflate(NoteActivity.this, R.layout.activity_note, null);
                         //如果选择了item显示黄色,未选择显示白色
                         if (current==arg0) {
                                  myView.setBackgroundColor(Color.YELLOW);
                         }
                         else {
                                  myView.setBackgroundColor(Color.WHITE);
                         }
                         TextView noteTextView=(TextView)myView.findViewById(R.id.note_notename);
                         TextView timeTextView=(TextView)myView.findViewById(R.id.note_notetime);
                         noteTextView.setText(notelist.get(arg0).getTitleString());
                         timeTextView.setText(notelist.get(arg0).getDateString());
                         return myView;
                 }
        }
        public boolean onContextItemSelected(MenuItem item) {
                 // TODO Auto-generated method stub
                 return super.onContextItemSelected(item);
        }
}


最后于 2020-1-27 被ZAESKY编辑 ,原因:
最新回复 (0)
返回