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 ...
随机推荐
- PDF抽取文字 C# with Adobe API
前提是PDF里面是有文字的! 一次性取得所有页内容: /// <summary> /// 改进前取得所有页的所有word /// </summary> /// <para ...
- 学习Swift -- 协议(上)
协议(上) 协议是Swift非常重要的部分,协议规定了用来实现某一特定工作或者功能所必需的方法和属性.类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能够满足协议要求 ...
- jq总结
总述 jQuery 框架提供了很多方法,但大致上可以分为3 大类: 获取jQuery 对象的方法 在jQuery 对象间跳转的方法 获取jQuery 对象后调用的方法 获取 jQuery 对象 是怎样 ...
- Python处理XML
在Python(以及其他编程语言)内有两种常见的方法处理XML:SAX(Simple API for XML)和DOM(Document Object Model,文档对象模型).SAX语法分析器读取 ...
- 10071 - Back to High School Physics
Problem B Back to High School Physics Input: standard input Output: standard output A particle has i ...
- uploadify 自定义按钮样式
uploadify是一款不错的JQUERY上传插件,但是FLASH按钮的外挂往往跟我们网页的设计不太搭配.一开始我还试图反编译uploadify.swf来修改其外观,结果发现反编译为FLA后里面没有任 ...
- Codeforces Round #206 (Div. 2)
只会做三个题: A:简单题,不解释: #include<cstdio> using namespace std; int k,d; int main() { scanf("%d% ...
- 【MongoDB】应用场景
24 Use Cases24.1 适合场景 Archiving and event logging 归档和日志记录 Document and Content Management Systems ...
- Bluetooth LE(低功耗蓝牙) - 第六部分(完)
在本系列前面的文章中我们已经了解了,在我们从一个TI SensorTag中获取温度和湿度数据之前,我们需要经历的各种步骤.在本系列中的最后一篇文章,我们将完成注册并接收SensorTag的通知,并接收 ...
- xlslib安装, aclocal-1.13: command not found, 安装升级autoconf-2.65.tar.gz, automake-1.13.tar.gz两个文件
问题1: $ make CDPATH="${ZSH_VERSION+.}:" && cd . && aclocal-1.13 -I m4 /bin/ ...