我在主页面创建的新建便签和显示全部便签的两个按钮,
点击新建便签的按钮就进入编辑新便签的界面,
点击全部便签进入一个显示所有便签的界面,但就在点击全部便签时出错了(全部便签里是一个列表视图,在里面点击一条便签进入编辑)
这是全部便签的代码
public class All_note extends ListActivity {
public final static String EXTRA_NOTE_ID = "com.example.android.notepad.NOTEID";
private NoteDbHelper _db = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_note);
getListView().setOnCreateContextMenuListener(this);
_db = new NoteDbHelper(this);
String[] dataColumns = new String[] {"title", "content"};
int[] viewIds = new int[] { R.id.title, R.id.summary };
Cursor cursor = _db.getAllNotesCursor();
SimpleCursorAdapter adapter
= new SimpleCursorAdapter(
this, // The Context for the ListView
R.layout.notelist_item, // Points to the XML for a list item
cursor, // The cursor to get items from
dataColumns,
viewIds
);
setListAdapter(adapter);
}
private void reloadNote() {
SimpleCursorAdapter adapter = (SimpleCursorAdapter)getListAdapter();
if (adapter != null) {
Cursor cursor = _db.getAllNotesCursor();
adapter.swapCursor(cursor);
}
}
@Override
public void onResume() {
super.onResume();
reloadNote();
}
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, NewNote.class);
intent.putExtra(EXTRA_NOTE_ID, (int)id);
startActivity(intent);
}
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
// Inflate menu from XML resource
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
// The data from the menu item.
AdapterView.AdapterContextMenuInfo info;
try {
// Casts the data object in the item into the type for AdapterView objects.
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
// If the object can't be cast, logs an error
Log.e("SimpleNotePad", "bad menuInfo", e);
// Triggers default processing of the menu item.
return false;
}
/*
* Gets the menu item's ID and compares it to known actions.
*/
switch (item.getItemId()) {
case R.id.context_delete:
int noteId = (int)info.id;
if (noteId > 0)
_db.deleteNote(noteId);
reloadNote();
// Returns to the caller and skips further processing.
return true;
default:
return super.onContextItemSelected(item);
}
}
}
主页面代码
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img1=(ImageView)findViewById(R.id.setButton0);
img1.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View V){
Intent intent=new Intent(MainActivity.this,SetActivity.class);
startActivity(intent);
}
});
ImageView img2=(ImageView)findViewById(R.id.newButton2);
img2.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View V){
Intent intent=new Intent(MainActivity.this,NewNote.class);
startActivity(intent);
}
});
ImageView img3=(ImageView)findViewById(R.id.allButton1);
img3.setOnClickListener(new ImageView.OnClickListener(){
public void onClick(View V){
Intent intent=new Intent(MainActivity.this,All_note.class);
startActivity(intent);
}
});
}
}
配置文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<activity
android:name=".Welcome"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="cn.itcast.one.GuideActivity"></activity>
<activity
android:name="cn.itcast.one.MainActivity"
android:label="@string/app_name"></activity>
<activity android:name="cn.itcast.set.SetActivity"></activity>
<activity android:name="cn.itcast.one.NewNote"></activity>
<activity android:name="cn.itcast.one.All_note"></activity>
</application>