【实例简介】
【实例截图】
【核心代码】
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.domain.Student;
import com.service.PersonService;
public class ShowActivity extends Activity {
private ListView list_students;
private List students;
private List> data;
private String[] dataFrom = new String[] { "item_id", "item_name",
"item_number", "item_age" };
private int[] dataTo = new int[] { R.id.item_id, R.id.item_name,
R.id.item_number, R.id.item_age };
private PersonService service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_layout);
//数据显示
list_students = (ListView) findViewById(R.id.list_show);
service = new PersonService(getApplicationContext());
try {
students = service.getAllData();
} catch (Exception e) {
e.printStackTrace();
}
data = getData(students);
SimpleAdapter adapter = new SimpleAdapter(this,data, R.layout.item_layout, dataFrom, dataTo);
list_students.setAdapter(adapter);
registerForContextMenu(list_students);//设置上下文菜单监听事件
//设置上下文菜单监听事件的第二种方式
/*list_students
.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
System.out.println("24141");
menu.setHeaderTitle("操作");
menu.add(0, 0, 0, "修改记录");
menu.add(0, 1, 0, "删除记录");
}
});*/
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("操作");
menu.add(0, 0, 0, "修改记录");
menu.add(0, 1, 0, "删除记录");
}
/**
* 为菜单添加事件
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
try {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
int p = info.position;
Map m = data.get(p);
String item_id = (String) m.get("item_id");
System.out.println("find*************");
switch (item.getItemId()) {
case 0:
System.out.println("find");
find(item_id);
return true;
case 1:
delete(item_id);
return true;
default:
System.out.println("-----up---------");
return super.onContextItemSelected(item);
}
} catch (Exception e) {
return super.onContextItemSelected(item);
}
}
/**
* Activity的resume事件
*/
@Override
protected void onResume() {
super.onResume();
try {
students = service.getAllData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
data = getData(students);
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(),
data, R.layout.item_layout, dataFrom, dataTo);
list_students.setAdapter(adapter);
}
/**
* 数据删除
*
* @param id
* @throws Exception
*/
public void delete(String item_id) {
try {
service.delete(item_id);
Toast.makeText(getApplicationContext(), "数据:" item_id " 删除成功!",
Toast.LENGTH_SHORT).show();
onResume();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "数据:" item_id " 删除失败!",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
/**
* 数据更新
*
* @param id
* @throws Exception
*/
public void find(String item_id) throws Exception {
Toast.makeText(getApplicationContext(), "数据:" item_id " 更新成功!",
Toast.LENGTH_SHORT).show();
System.out.println("-----up---------");
Student student = service.find(item_id);
Bundle bundle = new Bundle();
bundle.putSerializable("stu", student);
Intent intent = new Intent(this, UpdataActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
/**
* 数据获取
*
* @param stus
*
*/
private List> getData(List stus) {
List> da = new ArrayList>();
for (int i = 0; i < stus.size(); i ) {
Student stu = students.get(i);
Map map = new HashMap();
map.put("item_id", stu.getId());
map.put("item_name", stu.getName());
map.put("item_number", stu.getNumber());
map.put("item_age", stu.getAge());
da.add(map);
}
return da;
}
}