android 35 ListView增删改差
MainActivity
package com.sxt.day05_11; import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; import com.sxt.day05_11.entity.GeneralBean; public class MainActivity extends Activity {
ListView mlvGeneral;
List<GeneralBean> mGenerals;
GeneralAdapter mAdapter;
private static final int ACTION_DETAILS=0;
private static final int ACTION_ADD=1;
private static final int ACTION_DELETE=2;
private static final int ACTION_UPDATE=3; int mPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
setListener();
} private void setListener() {
mlvGeneral.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("选择以下操作")
.setItems(new String[]{"查看详情","添加数据","删除数据","修改数据"}, new OnClickListener() {//倒的包是import android.content.DialogInterface.OnClickListener;
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case ACTION_DETAILS:
showDetails(position);//这个position在mGenerals.get(position);调用,说明position是资源总条数范围,
break;
case ACTION_ADD: break;
case ACTION_DELETE:
mAdapter.remove(position);//调用适配器的删除方法
break;
case ACTION_UPDATE:
//启动修改的Activity,并将当前的军事家对象传递过去
Intent intent=new Intent(MainActivity.this, UpdateActivity.class);
intent.putExtra("general", mGenerals.get(position));//mGenerals.get(position)的对象要实现序列化接口
mPosition=position;
startActivityForResult(intent, ACTION_UPDATE);//要求返回结果,ACTION_UPDATE是requestCode
break;
}
} private void showDetails(int position) {
GeneralBean general=mGenerals.get(position);
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle(general.getName())
.setMessage(general.getDetails())
.setPositiveButton("返回", null);//直接关闭,没有响应事件
AlertDialog dialog = builder.create();
dialog.show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});
} private void initView() {
mlvGeneral=(ListView) findViewById(R.id.lvGeneral);
mAdapter=new GeneralAdapter(mGenerals, this);
mlvGeneral.setAdapter(mAdapter);
} private void initData() {
String[] names=getResources().getStringArray(R.array.general);
String[] details=getResources().getStringArray(R.array.details);
int[] resid={
R.drawable.baiqi,R.drawable.caocao,R.drawable.chengjisihan,
R.drawable.hanxin,R.drawable.lishimin,R.drawable.nuerhachi,
R.drawable.sunbin,R.drawable.sunwu,R.drawable.yuefei,
R.drawable.zhuyuanzhang
};
mGenerals=new ArrayList<GeneralBean>();
for (int i = 0; i < resid.length; i++) {
GeneralBean general=new GeneralBean(resid[i], names[i], details[i]);
mGenerals.add(general);
}
} class GeneralAdapter extends BaseAdapter{
List<GeneralBean> generals;
MainActivity context; public void remove(int position){//适配器移出方法,就是移除资源总数据,
generals.remove(position);
notifyDataSetChanged();//BaseAdapter的方法,执行后安卓系统会调用getView()方法重新绘制,
} public void add(GeneralBean general){
mGenerals.add(general);
notifyDataSetChanged();
} public void update(int position,GeneralBean general){
mGenerals.set(position, general);
notifyDataSetChanged();
} public GeneralAdapter(List<GeneralBean> generals, MainActivity context) {
super();
this.generals = generals;
this.context = context;
} @Override
public int getCount() {
return generals.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if(convertView==null){
convertView=View.inflate(context, R.layout.item_general, null);
holder=new ViewHolder();
holder.ivThumb=(ImageView) convertView.findViewById(R.id.ivThumb);
holder.tvName=(TextView) convertView.findViewById(R.id.tvName);
convertView.setTag(holder);
}else{//滚屏的时候重复利用convertView
holder=(ViewHolder) convertView.getTag();
}
GeneralBean general=generals.get(position);
holder.ivThumb.setImageResource(general.getResid());
holder.tvName.setText(general.getName());
return convertView;
} class ViewHolder{
ImageView ivThumb;
TextView tvName;
}
} @Override
//处理UpdateActivity返回的结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=RESULT_OK){//判断resultCode
return ;
}
switch (requestCode) {//判断requestCode
case ACTION_UPDATE:
GeneralBean general=(GeneralBean) data.getSerializableExtra("general");//获取修改完以后的对象
mAdapter.update(mPosition, general);//调用适配器的更新方法,mPosition是修改的对象在集合的索引
break;
case ACTION_ADD: break;
}
}
}
mainactivity页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ListView
android:id="@+id/lvGeneral"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </RelativeLayout>
修改Activity:
package com.sxt.day05_11; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView; import com.sxt.day05_11.entity.GeneralBean; public class UpdateActivity extends Activity {
//要修改对象的名字(控件显示),详细信息(控件显示),
EditText metName,metDetails;
ImageView mivThumb;//图片
int mPhotoId;//图片id
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
initView();
initData();
setListener();
} private void setListener() {
setOKClickListtener();
setCancelClickListener();
} private void setCancelClickListener() {
findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
} private void setOKClickListtener() {
findViewById(R.id.btnOK).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String details=metDetails.getText().toString();
String name=metName.getText().toString();
//不能根据图片控件获取图片的id,所以多用一个变量保存图片的id
GeneralBean general=new GeneralBean(mPhotoId, name, details);
Intent intent=new Intent(UpdateActivity.this, MainActivity.class);
intent.putExtra("general", general);
setResult(RESULT_OK, intent);//RESULT_OK是resultCode结果码
finish();//关闭当前页面
}
});
} //接收数据
private void initData() {
Intent intent = getIntent();
GeneralBean general=(GeneralBean) intent.getSerializableExtra("general");
//数据显示在控件里面
metDetails.setText(general.getDetails());
metName.setText(general.getName());
mivThumb.setImageResource(general.getResid());//图片是根据id获取的
mPhotoId=general.getResid();//保存图片id
} private void initView() {
//用布局实例化对象
metDetails=(EditText) findViewById(R.id.etDetails);
metName=(EditText) findViewById(R.id.etName);
mivThumb=(ImageView) findViewById(R.id.iv_updae_thumb);
} }
修改Activity页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/iv_updae_thumb"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:src="@drawable/baiqi"/>
<EditText 可编辑的输入框
android:id="@+id/etName"
android:layout_below="@id/iv_updae_thumb"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="白起"/>
<EditText
android:id="@+id/etDetails"
android:layout_width="match_parent"
android:layout_height="110dp"
android:text="@string/detail"
android:layout_toRightOf="@id/iv_updae_thumb"/> <Button
android:id="@+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:layout_below="@id/etDetails"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="放弃"
android:layout_below="@id/etDetails"
android:layout_marginTop="20dp"
android:layout_toRightOf="@id/btnOK"
android:layout_marginLeft="50dp"/> </RelativeLayout>
item_general.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="horizontal" > 横向 <ImageView
android:id="@+id/ivThumb"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="fitXY" 自动缩放
android:src="@drawable/baiqi"/> <TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center_vertical" 内部垂直居中
android:textSize="20sp"
android:text="白起"
android:layout_marginLeft="10dp"/>
</LinearLayout>
android 35 ListView增删改差的更多相关文章
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- 利用SQLite在android上实现增删改查
利用SQLite在android上实现增删改查 方法: 一.直接利用database.execSQL()方法输入完整sql语句进行操作 这种方法适用于复杂的sql语句,比如多表查询等等 这里适合于增删 ...
- android 数据库的增删改查的另一种方式
老师笔记 # 3 Android下另外一种增删改查方式 1.创建一个帮助类的对象,调用getReadableDatabase方法,返回一个SqliteDatebase对象 2.使用Sq ...
- android 数据库的增删改查
主java package com.itheima.crud; import android.app.Activity; import android.content.Context; import ...
- IBatis增删改差的实现以及注意点
此次进讲述对表操作的实现细节.废话不多说,代码见真章. <?xml version="1.0" encoding="utf-8" ?> <sq ...
- Android学习--------实现增删改查数据库操作以及实现相似微信好友对话管理操作
版权声明:本文为博主原创文章,转载请注明原文地址.谢谢~ https://blog.csdn.net/u011250851/article/details/26169409 近期的一个实验用到东西挺多 ...
- hibernate课程 初探单表映射3-5 hibernate增删改查
本节简介: 1 增删改查写法 2 查询load和查询get方法的区别 3 demo 1 增删改查写法 增加 session.save() 修改 session.update() 删除 session. ...
- Android SQLite数据库增删改查操作
一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...
- mybatis06 增删改差 源码
user.java package cn.itcast.mybatis.po; import java.util.Date; public class User { private int id; p ...
随机推荐
- Source kit service terminated Editor functionality temporarily limited
这下可好. Source kit service terminated Editor functionality temporarily limited 运行以下代码出现了以上的提示...另外,还压根 ...
- c++清除输入缓冲区之 sync() vs ignore()
最近在写程序的时候总是不注意输入缓冲区内是否还有东西,导致出现了一些异常,调试了半天.所以来上一贴,学习注意,引以为戒! http://blog.chinaunix.net/uid-21254310- ...
- APCS
arm汇编程序中,R0,R1,R2,R3,R12都是作为中间寄存器,而R4-R11是不能随便使用的,暂时我还不知它们的用途.所以,中间寄存器,在程序运行的开始处与结束的时候值是可以不一样的,也就是说中 ...
- HTML5之一HTML5简介
1.什么是HTML5? HTML5是HTML的新一代标准.以前版本的HTML标准4.01发布于1999. 自1999年以后,web已经有了翻天覆地的变化. 实际上HTML5仍旧是开发中的一个标准.但是 ...
- 共享式以太网与交换式以太网的性能比较(OPNET网络仿真实验)
一.实验目的 比较共享式以太网和交换式以太网在不同网络规模下的性能. 二.实验方法 使用opnet来创建和模拟网络拓扑,并运行分析其性能. 三.实验内容 3.1 实验设置(网络拓扑.参数设置. ...
- 自己写的carousel
可以 function appendRight() { //alert("right"); lastItem = itemsRight[urls.length - ]; first ...
- The Maximum Number of Strong Kings
poj2699:http://poj.org/problem?id=2699 题意:n个人,进行n*(n-1)/2场比赛,赢一场则得到一分.如果一个人打败了所有比他分数高的对手,或者他就是分数最高的, ...
- Qt写的截图软件包含源代码和可执行程序
http://blog.yundiantech.com/?log=blog&id=14 Qt写的截图软件包含源代码和可执行程序 http://download.csdn.net/downloa ...
- configure: error: cannot find protoc, the Protocol Buffers compiler
centos 6 安装mosh 1.2 2012-05-07 17:21:41标签:centos mosh 关于mosh(引用于) 芬兰研究员Tatu Ylönen于1995年设计出最早的SSH协议, ...
- linux远程管理工具
一.常见的远程管理控制方式主要有以下几种 ①RDP(remote desktop protocol)协议 远程桌面协议,我们常用的windows操作系统就是的远程桌面管理就是基于该协议的. ②teln ...