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增删改差的更多相关文章

  1. Android SQLite 数据库 增删改查操作

    Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...

  2. 利用SQLite在android上实现增删改查

    利用SQLite在android上实现增删改查 方法: 一.直接利用database.execSQL()方法输入完整sql语句进行操作 这种方法适用于复杂的sql语句,比如多表查询等等 这里适合于增删 ...

  3. android 数据库的增删改查的另一种方式

    老师笔记 # 3 Android下另外一种增删改查方式         1.创建一个帮助类的对象,调用getReadableDatabase方法,返回一个SqliteDatebase对象 2.使用Sq ...

  4. android 数据库的增删改查

    主java package com.itheima.crud; import android.app.Activity; import android.content.Context; import ...

  5. IBatis增删改差的实现以及注意点

    此次进讲述对表操作的实现细节.废话不多说,代码见真章. <?xml version="1.0" encoding="utf-8" ?> <sq ...

  6. Android学习--------实现增删改查数据库操作以及实现相似微信好友对话管理操作

    版权声明:本文为博主原创文章,转载请注明原文地址.谢谢~ https://blog.csdn.net/u011250851/article/details/26169409 近期的一个实验用到东西挺多 ...

  7. hibernate课程 初探单表映射3-5 hibernate增删改查

    本节简介: 1 增删改查写法 2 查询load和查询get方法的区别 3 demo 1 增删改查写法 增加 session.save() 修改 session.update() 删除 session. ...

  8. Android SQLite数据库增删改查操作

    一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...

  9. mybatis06 增删改差 源码

    user.java package cn.itcast.mybatis.po; import java.util.Date; public class User { private int id; p ...

随机推荐

  1. postgresql 的触发器

    今天编写了一个触发器 功能: 有两个表,当一个表的字段有所改动的时候,另一个表跟着改动 CREATE OR REPLACE FUNCTION process_emp_audit() RETURNS T ...

  2. opencv 构造训练器

    D:/face   构造face训练器为例 一:样本创建 训练样本分为正例样本和反例样本,其中正例样本是指待检目标样本,反例样本指其它任意图片. 负样本可以来自于任意的图片,但这些图片不能包含目标特征 ...

  3. MAC 使用Jetbrains's产品

    Jetbrains's MAC 使用 ./gradle fatjar 或者 ./gradlew.sh fatjar java -jar build/lib/xx.jar 链接: http://pan. ...

  4. EOF

    Process to end of file 就是处理到文件的结束 以经典的 A + B Problem 为例 把每一行的两个数字加起来,然后打印出来,直到文件末尾 c 语言表示:

  5. eclipse的使用、优化配置

    一.简介 eclipse 可谓是Java开发界的神器,基本占据了大部分的Java开发市场,而且其官方还对其他语言提供支持,如C++,Ruby,JavaScript等等.为 什么使用它?我想离不开下面的 ...

  6. SAAS相关技术要点

    这篇文章本来是我们开发组内部用的一个小文档.因为我们公司以前没有做SAAS的经验,就成立了一个小组做一做这方面的技术前探,我是成员之一.这篇文档想从宏观的层面把开发一个SAAS应用所要用到的技术点稍微 ...

  7. Technology Trader

    zoj2071:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2071 题意:题意一些零件,每一个零件会有一个花费,然后用这 ...

  8. Party

    hdu3062:http://acm.hdu.edu.cn/showproblem.php?pid=3062 题意:中文题. 题解:很明显的2-sat.然后要深刻理解命题和逆否命题.如这一题,c1,c ...

  9. 嵌入式C语言头文件的建立与使用

    如何正确编写 C 语言头文件和与之相关联的 c 源程序文件,这首先就要了解它们的各自功能. 要理解 C 文件与头文件(即.h)有什么不同之处,首先需要弄明白编译器的工作过程. 一般说来编译器会做以下几 ...

  10. SQL语句 远程操作数据库

    --远程操作数据库SQL语句exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '211.81.251.85 'exec sp_addlinkedsr ...