android之listView之增删改查

主显示布局以及代码:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ListView 
        android:id="@+id/lvGenerals"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#ccc"
        android:dividerHeight="5dp"
        />

</RelativeLayout>

java代码:

package com.litsoft.main;

import java.util.ArrayList;
import java.util.List;

import com.litsoft.entity.General;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

private ListView lvGenerals;//listView对象
private List<General> generals;//将军的集合
private GeneralAdapter generalAdaper;//适配器
private static final int VIEW_DETAIL = 0;//查看详情
private static final int VIEW_DETELE = 1;//删除操作
private static final int VIEW_ADD= 2;//添加操作
private static final int VIEW_UPDATE = 3;//修改操作

private static final int ACTION_UPDATE =4;
private static final int ACTION_ADD =5;
private int mPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
setListener();
}

private void initView() {
// TODO Auto-generated method stub
lvGenerals = (ListView) findViewById(R.id.lvGenerals);
generalAdaper = new GeneralAdapter(this,generals);
lvGenerals.setAdapter(generalAdaper);

}
private void initData() {
// TODO Auto-generated method stub
generals = new ArrayList();//初始化集合对象
int [] images = new int [] {
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
};//图片资源
String [] names = getResources().getStringArray(R.array.general);//姓名资源
String [] details = getResources().getStringArray(R.array.details);//详情资源
for(int i=0;i<names.length;i++){
General general = new General(images[i],names[i],details[i]);
generals.add(general);
}

}

private void setListener() {
this.lvGenerals.setOnItemLongClickListener(new OnItemLongClickListener() {

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("请选择以下操作").setItems(new String[] {"查看详情","删除操作","增加操作","修改操作"}, new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch(which){
case VIEW_DETAIL:
showDetail(position);
break;
case VIEW_DETELE:
generalAdaper.remove(position);
break;
case VIEW_ADD:
Intent intent1 = new Intent(MainActivity.this,AddActivity.class);
startActivityForResult(intent1, ACTION_ADD);
break;
case VIEW_UPDATE:
mPosition = position;
Intent intent = new Intent(MainActivity.this,UpdateActivity.class);
intent.putExtra("general",generals.get(position));
startActivityForResult(intent, ACTION_UPDATE);
break;
}
}

private void showDetail(int position) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
General general = generals.get(position);
builder.setTitle(general.getName()).setMessage(general.getDetail())
.setPositiveButton("返回", null);
AlertDialog dialog = builder.create();
dialog.show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
}
});

}

class GeneralAdapter extends BaseAdapter{
private Context context;
private List<General> generals;
public void remove(int position){
generals.remove(position);
this.notifyDataSetChanged();
}
public void update(int position,General general){
generals.set(position, general);
this.notifyDataSetChanged();
}
public void add(General general){
generals.add(general);
this.notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return generals.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return generals.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public GeneralAdapter(Context context, List<General> generals) {
super();
this.context = context;
this.generals = generals;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Viewholder viewholder = null;
if(convertView == null){
viewholder = new Viewholder();
convertView = View.inflate(context, R.layout.general, null);
viewholder.ivThumb = (ImageView) convertView.findViewById(R.id.ivThumb);
viewholder.tvName = (TextView) convertView.findViewById(R.id.tvName);
convertView.setTag(viewholder);
}else{
viewholder = (Viewholder) convertView.getTag();
}
try{
General general = this.generals.get(position);
viewholder.ivThumb.setImageResource(general.getImageSrc());
viewholder.tvName.setText(general.getName());
}catch(Exception e){
Log.i("main", e.getMessage());
}
return convertView;
}

class Viewholder{
ImageView ivThumb;
TextView tvName;
}

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode!=RESULT_OK){
return;
}
switch (requestCode) {
case ACTION_UPDATE:
General general = (General) data.getSerializableExtra("general");
generalAdaper.update(mPosition, general);
break;
case ACTION_ADD:
General general1 = (General) data.getSerializableExtra("general");
generalAdaper.add(general1);
break;
}
}
}

修改操作的布局以及代码:

activity_update.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<ImageView 
   android:id="@+id/ivupdate"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:src="@drawable/baiqi"/>
<EditText 
   android:id="@+id/etupdatename"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
     />
<EditText 
   android:id="@+id/etudpatedetail"
   android:layout_width="match_parent"
   android:layout_height="110dp"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="center_horizontal">
    <Button 
        android:id="@+id/updateBt"
        android:text="修改"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
     <Button 
         android:id="@+id/cancleBt"
         android:text="放弃"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
     </LinearLayout>

</LinearLayout>

java代码:

package com.litsoft.main;

import com.litsoft.entity.General;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class UpdateActivity extends ActionBarActivity {
private EditText etupdatename,etudpatedetail;
private General general = null;
private int mImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
initData();
initView();
setlistenser();
}
private void setlistenser() {
// TODO Auto-generated method stub
findViewById(R.id.cancleBt).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});

findViewById(R.id.updateBt).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String name = etupdatename.getText().toString();
String detail = etudpatedetail.getText().toString();
General general = new General(mImage,name,detail);
Intent intent = new Intent(UpdateActivity.this,MainActivity.class);
intent.putExtra("general", general);
setResult(RESULT_OK, intent);
finish();
}
});
}
private void initView() {
// TODO Auto-generated method stub
etupdatename = (EditText) findViewById(R.id.etupdatename);
etudpatedetail = (EditText) findViewById(R.id.etudpatedetail);
etupdatename.setText(general.getName());
etudpatedetail.setText(general.getDetail());
}
private void initData() {
// TODO Auto-generated method stub
Intent intent = getIntent();
general = (General) intent.getSerializableExtra("general");
mImage = general.getImageSrc();
}

}

添加操作布局以及代码 :activity_add.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
   >
    <TextView 
        android:text="姓名"
         android:layout_width="80dp"
   android:layout_height="80dp"/>

<EditText 
   android:id="@+id/etaddname"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
     />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    >
<TextView 
   android:text="详细介绍"
    android:layout_width="80dp"
   android:layout_height="80dp"/>
<EditText 
   android:id="@+id/etadddetail"
   android:layout_width="match_parent"
   android:layout_height="110dp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:gravity="center_horizontal">
    <Button 
        android:id="@+id/addBt"
        android:text="添加"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
     <Button 
         android:id="@+id/addcancleBt"
         android:text="放弃"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
     </LinearLayout>

</LinearLayout>

java代码:

package com.litsoft.main;

import com.litsoft.entity.General;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class AddActivity extends ActionBarActivity {

private EditText etaddname,etadddetail;
private General general = null;
private int mImage = R.drawable.baiqi;//测试写成固定的了
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
initView();
setlistenser();
}
private void setlistenser() {
// TODO Auto-generated method stub
findViewById(R.id.addcancleBt).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});

findViewById(R.id.addBt).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String name = etaddname.getText().toString();
String detail = etadddetail.getText().toString();
General general = new General(mImage,name,detail);
Intent intent = new Intent(AddActivity.this,MainActivity.class);
intent.putExtra("general", general);
setResult(RESULT_OK, intent);
finish();
}
});
}
private void initView() {
// TODO Auto-generated method stub
etaddname = (EditText) findViewById(R.id.etaddname);
etadddetail = (EditText) findViewById(R.id.etadddetail);
}

}

其他 :

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="80dp"
        android:layout_height="80dp"
        android:src="@drawable/baiqi"/>
    <TextView 
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:text="白起"
        android:textSize="20sp"
        android:gravity="center_vertical"
        android:layout_marginLeft="20dp"
        />

</LinearLayout>

java类:General.java

package com.litsoft.entity;

import java.io.Serializable;

public class General implements Serializable{
private int imageSrc;
private String name;
private String detail;
public int getImageSrc() {
return imageSrc;
}
public void setImageSrc(int imageSrc) {
this.imageSrc = imageSrc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
@Override
public String toString() {
return "General [imageSrc=" + imageSrc + ", name=" + name + ", detail="
+ detail + "]";
}
public General(int imageSrc, String name, String detail) {
super();
this.imageSrc = imageSrc;
this.name = name;
this.detail = detail;
}

}

效果:

时间: 2024-10-26 02:46:00

android之listView之增删改查的相关文章

【Visual Basic】列表控件ListView的增删改查、模态对话框、禁止窗口调整大小

列表控件ListView是窗体中核心组件之一,在各种窗体程序得到广泛应用.在<[mfc]学生信息管理,实现List控件节点的增删改查>(点击打开链接)中极其难以实现列表控件ListView的增删改查,在vb中可以轻松实现,下面举一个列子来说明这个问题. 如上图的一个ListView控件,点击"添加"按钮,在弹出的模态窗口,可以为ListView控件添加相应的项. 通过"删除"按钮可以删除选中的项.通过"修改"按钮,选中的项的值将会传递

[Android] SQLite数据库之增删改查基础操作

    在编程中常常会遇到数据库的操作,而Android系统内置了SQLite,它是一款轻型数据库,遵守事务ACID的关系型数据库管理系统,它占用的资源非常低,可以支持Windows/Linux/Unix等主流操作系统,同一时候可以跟非常多程序语言如C#.PHP.Java等相结合.以下先回想SQL的基本语句,再讲述Android的基本操作. 一. adb shell回想SQL语句     首先,我感觉自己整个大学印象最深的几门课就包含<数据库>,所以想先回想SQL增删改查的基本语句.而在And

【Android】Sqlite数据库增删改查

Android系统内置一个Sqlite数据库,如果app需要使用Sqlite数据库数据库存储数据,Android会为此app生成一个.db文件.这个数据库在data/data/<package_name>/databases里面,其中<package_name>为该安卓app的工程包名,这个目录必须root后才能看到.在Windows,单机的应用程序,存储数据,基本放到一个文件里面,正如游戏的存档,基本就是把当前的游戏状态存到一个用户很难找到的文件里面.每次存档读档就是一个从这个存

Android下数据库操作——增删改查

Android下数据库第一种方式增删改查 1.创建一个帮助类的对象,调用getReadableDatabase方法,返回一个SqliteDatebase对象 2.使用SqliteDatebase对象调用execSql()做增删改,调用rawQuery方法做查询. 特点:增删改没有返回值,不能判断sql语句是否执行成功.sql语句手动写,容易写错 private MySqliteOpenHelper mySqliteOpenHelper;     public InfoDao(Context co

Android 图书管理实现增删改查

目录结构如下: java代码>> Book.java BookActivity.java BookAdapter.java MainActivity.java MyDatabaseHelper.java UpdateBookActivity.java layout>> activity_book.xml activity_main.xml activity_update_book.xml book_item.xml Book.java package com.example.p22

Android中SQLite的增删改查

首先我们创建一个数据库帮助类PersonSQLLiteOpenHelper,单词拼写有点小错误.. 让这个类继承SQLiteOpenHelper package com.example.android03db.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; i

安卓 Android 简单数据库(增删改查)

<Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除"/> <Button android:id="@+id/update_btn" android:layout_width="wr

【Visual Basic】vb6的ListView控件,对Access2003数据库的增删改查,判断是否有中文、多窗体操作

vb6对Access2003数据库的增删改查并不复杂,可以通过ado对象轻松完成,下面举个小例子,同时说明vb6中的ListView控件的使用.虽然在<[Visual Basic]列表控件ListView的增删改查.模态对话框.禁止窗口调整大小>曾经对VB.NET的ListView控件进行详细的说明,但是证明微软就是个坑爹货,vb6对于ListView实现的代码居然跟VB.NET有着彻底的不同,似乎换了一门语言似得的.改代码什么的最讨厌的. 首先,在vb6生成的工程文件夹中有着一个db1.md

【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建

数据库的创建和sql语句增删改查 1. 加载驱动. 2. 连接数据库. 3. 操作数据库. 创建表: create table person( _id integer primary key, name varchar(20), age integer ); 添加: insert into person(name, age) values('lisi', 19); 删除: delete from person where _id = 1; 修改: update person set name =