安卓学习-Bomb-便签案例

image_thumb9

imageimageimageimage

清单文件:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.bmobnote">

<!-- 允许联网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 获取GSM(2g)、WCDMA(联通3g)等网络状态的信息 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 获取wifi网络状态的信息 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- 获取sd卡写的权限,用于文件上传和下载 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 允许读取手机状态 用于创建BmobInstallation -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".NoteListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteDetailActivity" />
<activity android:name=".NoteNewActivity"></activity>
</application>

</manifest>

Note.java

package com.example.administrator.bmobnote;

import cn.bmob.v3.BmobObject;

/**
* Created by Goddess on 2016/4/16.
*/
public class Note extends BmobObject {
private String content;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}

NoteListActivity.java

package com.example.administrator.bmobnote;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;

public class NoteListActivity extends AppCompatActivity {

private static final int DEL_ITEM = 0x1;
private List<Note> notes = new ArrayList<>();
private ListView listView;
NoteAdapter na;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_list);
// 初始化 Bmob SDK
// 使用时请将第二个参数Application ID替换成你在Bmob服务器端创建的Application ID
Bmob.initialize(this, "220f35ab749970b5e7a6d3cd81d19e22");
listView = (ListView) findViewById(R.id.listView_note);
loadData();

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView_content = (TextView) view.findViewById(R.id.textView_content);
String content = textView_content.getText().toString();
String objectid = (String) view.getTag();
Intent intent = new Intent(NoteListActivity.this, NoteDetailActivity.class);
intent.putExtra("content", content);
intent.putExtra("objectid", objectid);
startActivity(intent);
}
});

registerForContextMenu(listView);
}

@Override
protected void onResume() {
super.onResume();
loadData();


}

private void loadData() {
BmobQuery<Note> query = new BmobQuery<>();
query.setLimit(50);//限定查的记录条数
query.findObjects(this, new FindListener<Note>() {
//异步的过程
@Override
public void onSuccess(List<Note> list) {
notes = list;
System.out.println(notes.size());
na = new NoteAdapter(NoteListActivity.this, notes);
listView.setAdapter(na);
}

@Override
public void onError(int i, String s) {

}
});
}


static class NoteAdapter extends BaseAdapter {
private Context context;
private List<Note> list;

public NoteAdapter(Context context, List<Note> list) {
this.context = context;
this.list = list;
}

@Override
public int getCount() {
return this.list.size();
}

@Override
public Object getItem(int position) {
return this.list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_note_item, null);

}
Note note = this.list.get(position);
TextView content=(TextView) convertView.findViewById(R.id.textView_content);
content.setText(note.getContent());
convertView.setTag(note.getObjectId());
return convertView;
}

static class ViewHolder {
TextView content;
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_note_list, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_note:
Intent intent = new Intent(this, NoteNewActivity.class);
startActivity(intent);
break;
default:
break;
}
return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(1, DEL_ITEM, 100, "删除");

}

@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DEL_ITEM:
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
System.out.println(info);
View view = info.targetView;
String objectid= (String) view.getTag();
Note note=new Note();
note.delete(this, objectid, new DeleteListener() {
@Override
public void onSuccess() {
loadData();
}

@Override
public void onFailure(int i, String s) {

}
});
break;
default:
break;
}
return super.onContextItemSelected(item);
}
}

NoteNewActivity.java

package com.example.administrator.bmobnote;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;

import cn.bmob.v3.listener.SaveListener;

public class NoteNewActivity extends AppCompatActivity {
private EditText editText_content;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_new);
editText_content = (EditText) findViewById(R.id.editText_content);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Note note = new Note();
String content = editText_content.getText().toString();
if (!TextUtils.isEmpty(content)) {
note.setContent(content);
note.save(this, new SaveListener() {
@Override
public void onSuccess() {
}

@Override
public void onFailure(int i, String s) {

}
});
}

}
return super.onKeyDown(keyCode, event);
}
}

NoteDetailActivity.java

package com.example.administrator.bmobnote;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.EditText;

import cn.bmob.v3.listener.UpdateListener;

public class NoteDetailActivity extends AppCompatActivity {

private EditText editText_content;
private String objectid;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_detail);
objectid = getIntent().getStringExtra("objectid");
editText_content = (EditText) findViewById(R.id.editText_content);
editText_content.setText(getIntent().getStringExtra("content"));
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Note note = new Note();
String content = editText_content.getText().toString();
if (!TextUtils.isEmpty(content)) {
note.setContent(content);
note.update(this, objectid, new UpdateListener() {
@Override
public void onSuccess() {
}

@Override
public void onFailure(int i, String s) {

}
});
}

}
return super.onKeyDown(keyCode, event);
}
}

布局:

activity_note_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".NoteListActivity">


<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView_note"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

activity_note_new.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.bmobnote.NoteNewActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/editText_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

activity_note_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.bmobnote.NoteDetailActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>

layout_note_item.xml

<?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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<TextView
android:id="@+id/textView_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text" />
</LinearLayout>

menu_note_list.xml

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
tools:context=".NoteListActivity"
>

<item android:id="@+id/add_note" android:title="添加"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>

转载于:https://my.oschina.net/xiaofeiandroid/blog/661251

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值