结对项目——MyBook功能

MyBook功能和界面
MyBook是这个程序的主界面,这个界面中有很多菜单向各个功能跳转。这里菜单可以实现电子书的添加导入,删除,查看电子书列表的功能,还有退出提示功能。
功能代码

public class MyBook extends BaseActivity {
	private BookManager bookManager;
	private ArrayList<Book> books;
	private GridView bookShelf;
	private ProgressDialog dialog;

	private int selectedIndex = -1;

	public static int width, height;

	public static Bitmap itemBackground;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_book);
		Display defaultDisplay = getWindowManager().getDefaultDisplay();
		width = defaultDisplay.getWidth();
		height = defaultDisplay.getHeight();
		itemBackground = ImageUtil.getBitmap(this, R.drawable.cover_txt, width / 4, height / 4);

		System.out.println("MyBook width:" + MyBook.width);
		System.out.println("MyBook height:" + MyBook.height);

		bookManager = new BookManager(this);
		bookShelf = (GridView) findViewById(R.id.bookShelf);
		
		loadData();

		
		bookShelf.setOnItemClickListener(new OnItemClickListener() {
			
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				selectedIndex = position;
				readBook();
			}
		});
		
		bookShelf.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				selectedIndex = position;
				return false;
			}
		});
		
		registerForContextMenu(bookShelf);
	}

	// 阅读
	private void readBook() {
		dialog = ProgressDialog.show(MyBook.this, "温馨提示", "正在加载文件", true);
		Book book = books.get(selectedIndex);
		File file = new File(book.getFilePath());
		if (!file.exists()) {// 文件不存在
			ToastUtils.toast(this, "文件不存在");
			dialog.dismiss();
			return;
		}
		Long id = book.getId();
		Intent intent = new Intent(this, ReadBook.class);
		intent.putExtra("bookId", id);
		dialog.dismiss();
		startActivity(intent);
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.my_book, menu);
		return super.onCreateOptionsMenu(menu);
	}

	// 菜单的点击事件
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_importbook:
			Intent intent = new Intent(this, ImportBook.class);
			startActivityForResult(intent, REQUEST_CODE_IMPORT_BOOK);
			break;
		case R.id.menu_help:
			//跳转到viewpager页面
			Intent intent1 = new Intent(MyBook.this, ViewPagerActivity.class);
			startActivity(intent1);
			break;
		case R.id.menu_setting:
			//跳转到设置页面
			Intent intent2 = new Intent(MyBook.this, SettingActivity.class);
			startActivity(intent2);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_IMPORT_BOOK) {
			loadData();
			ToastUtils.toast(this, "添加图书成功");
		}
	}

	// 加载数据到ListView
	private void loadData() {
		books = bookManager.getAll();
		Log.d(TAG, "books.size:" + books.size());
		ShlefAdapter adapter = new ShlefAdapter();
		bookShelf.setAdapter(adapter);
	}

	class ShlefAdapter extends BaseAdapter {

		
		public int getCount() {
			return books.size();
		}

		
		public Book getItem(int position) {
			return books.get(position);
		}

		
		public long getItemId(int position) {
			return getItem(position).getId();
		}

		
		public View getView(int position, View contentView, ViewGroup arg2) {
			Book book = getItem(position);
			contentView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.my_book_item, null);
			TextView view = (TextView) contentView.findViewById(R.id.bookName);
			view.setText(book.getName());
			BitmapDrawable bitmapDrawable = new BitmapDrawable(itemBackground);
			view.setBackgroundDrawable(bitmapDrawable);
			return contentView;
		}

	}

	// 创建上下文菜单
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		menu.setHeaderTitle("上下文菜单");
		MenuInflater inflater = new MenuInflater(this);
		inflater.inflate(R.menu.menu_book, menu);
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	// 上下文菜单的点击事件
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.read:
			readBook();
			break;
		case R.id.update:
			
			Long id = books.get(selectedIndex).getId();
			Intent intent = new Intent(this, BookFormActivity.class);
			intent.putExtra("bookId", id);
			startActivityForResult(intent, BOOK_UPDATE);
			break;
		case R.id.delete:
			Long id1 = books.get(selectedIndex).getId();
			bookManager.delete(id1);
			ToastUtils.toast(this, R.string.msg_delete_success);
			loadData();
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	// 改变主页面的返回事件
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			dialog();
			return false;
		}
		return false;
	}

	private void dialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("温馨提示");
		builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				MyBook.this.finish();
				// 返回home
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_MAIN);
				intent.addCategory(Intent.CATEGORY_HOME);
				startActivity(intent);
			}
		});
		builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}

界面

posted @ 2017-06-18 14:59  夜深人静时  阅读(256)  评论(0编辑  收藏  举报