码迷,mamicode.com
首页 > 移动开发 > 详细

android之listView之增删改查

时间:2014-12-07 06:48:35      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:android

主显示布局以及代码:

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;
}


}

效果:

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

本文出自 “追随心的彼岸” 博客,谢绝转载!

android之listView之增删改查

标签:android

原文地址:http://qa962839575.blog.51cto.com/6819179/1587139

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!