package com.menu;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class hellomenu extends ListActivity {
public static final int MENU_ITEM_DELETE = Menu.FIRST;
public static final int MENU_ITEM_INSERT = Menu.FIRST + 1;
private String note_name;
private ArrayList note_array;
/*
* private static final String[] PROJECTION = new String[] { Notes._ID, // 0
* Notes.TITLE, // 1 };
*/
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.listnote);
Cursor cursor;
note_array = new ArrayList();
for (int i = 0; i < 3; i++) {
note_array.add("test" + Integer.toString(i));
}
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, note_array);
getListView().setOnCreateContextMenuListener(this);
setListAdapter(adapter);
// this.registerForContextMenu(getListView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, MENU_ITEM_INSERT, 0, R.string.menu_insert).setShortcut('3',
'a').setIcon(android.R.drawable.ic_menu_add);
// Generate any additional actions that can be performed on the
// overall list. In a normal install, there are no additional
// actions found here, but this allows other applications to extend
// our menu with their own actions.
Intent intent = new Intent(null, getIntent().getData());
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu
.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
new ComponentName(this, hellomenu.class), null, intent,
0, null);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ITEM_INSERT:
// Launch activity to insert a new item
String ls_note;
Intent intent_add = new Intent(this, note_edit.class);
startActivityForResult(intent_add, 100);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
Log.e("NotesList", "bad menuInfo", e);
return;
}
String note[] = new String[3];
note[1] = getListAdapter().getItem(info.position).toString();
if (note[1] == null) {
// For some reason the requested item isn't available, do nothing
return;
}
// Setup the menu header
menu.setHeaderTitle(note[1]);
// Add a menu item to delete the note
menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_delete);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
final boolean haveItems = getListAdapter().getCount() > 0;
// If there are any notes in the list (which implies that one of
// them is selected), then we need to generate the actions that
// can be performed on the current selection. This will be a combination
// of our own specific actions along with any extensions that can be
// found.
if (haveItems) {
// This is the selected item.
// String note_c=getListView().getSelectedItem().toString();
// Build menu... always starts with the EDIT action...
/*
* Intent[] specifics = new Intent[1]; specifics[0] = new
* Intent(Intent.ACTION_EDIT, null); MenuItem[] items = new
* MenuItem[1]; // ... is followed by whatever other actions are
* available... Intent intent = new Intent(this, hellomenu.class);
* intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
* menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null,
* specifics, intent, 0, items); // Give a shortcut to the edit
* action. if (items[0] != null) { items[0].setShortcut('1', 'e'); } }
* else { menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
*/
}
return true;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterView.AdapterContextMenuInfo info;
try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
} catch (ClassCastException e) {
return false;
}
switch (item.getItemId()) {
case MENU_ITEM_DELETE: {
try {
// ��ʾ�����м�¼���ݻ�ȡ����
ListView select_view = (ListView) info.targetView.getParent();
note_name = select_view.getItemAtPosition(info.position)
.toString();
} catch (ClassCastException e) {
return false;
}
// info.idΪ�����м�¼id
note_array.remove((int) info.id);
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, note_array);
setListAdapter(adapter);
return true;
}
}
return super.onContextItemSelected(item);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String action = getIntent().getAction();
if (Intent.ACTION_PICK.equals(action)
|| Intent.ACTION_GET_CONTENT.equals(action)) {
// The caller is waiting for us to return a note selected by
// the user. The have clicked on one, so return it now.
//�м�¼���ݻ�ȡ��ʾ
note_name = getListView().getItemAtPosition(position).toString();
} else {
// Launch activity to view/edit the currently selected item
String ls_note;
Intent intent_add = new Intent(hellomenu.this, note_edit.class);
ls_note = getListAdapter().getItem(position).toString();
intent_add.putExtra("note", ls_note);
startActivityForResult(intent_add, 100);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == 100) {
String ls_note, ls_no[];
Bundle b = data.getExtras();
ls_note = b.getString("note");
ls_no = b.getStringArray("note");
note_array.add(ls_note);
ListAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, note_array);
setListAdapter(adapter);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
HelloMenu.rar (30个子文件)
HelloMenu
.project 845B
default.properties 362B
bin
HelloMenu.apk 18KB
com
menu
R$string.class 499B
R$layout.class 400B
R$drawable.class 370B
hellomenu.class 6KB
note_edit$1.class 1KB
R.class 461B
note_edit.class 1KB
R$attr.class 310B
Note.class 821B
SaveObjectArray.class 1KB
R$id.class 420B
resources.ap_ 11KB
classes.dex 9KB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 227B
AndroidManifest.xml 913B
src
com
menu
note_edit.java 1KB
hellomenu.java 7KB
arrayprovider.java 2KB
res
values
strings.xml 328B
layout
listnote.xml 393B
noteedit.xml 865B
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
.classpath 280B
gen
com
menu
R.java 1KB
assets
共 30 条
- 1
blue6626
- 粉丝: 0
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页