android listview高级,android ----- ListView的高级应用

在此我不在赘述android API中的内容,只是实际的经验,拿出来共享一下。

常见的ListView的显示风格都是调用系统定义的样式,如:android.R.layout.sample_list_item1,这种风格在大多开发过程中不能满足用户的要求,如果要使得包含图片,文本,以及其它的形式,上述格式明显不可以了,下面记录了一种比较好用的格式,具体的代码如下:

/res/values/string.xml

encoding="utf-8"?>

Hello World,

MyListActivity!

name="app_name">MyList

name="name1">tianya

name="name2">liwei

name="phone1">123456

name="phone2">122456

name="phone3">132456

/res/layout/main.xml

encoding="utf-8"?>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="300dip"

android:id="@+id/listView01"

android:scrollbars="vertical"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginTop="20px"

>

android:layout_width="150px"

android:layout_height="wrap_content"

android:id="@+id/addUserBtn"

android:text="添加用户"

android:layout_marginLeft="100px"

/>

/res/layout/dialog.xml

encoding="utf-8"?>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:stretchColumns="1"

android:shrinkColumns="0"

android:orientation="vertical"

android:id="@+id/dialog_table"

>

android:orientation="horizontal">

android:text="\t姓名" android:layout_width="80dp"

android:layout_height="wrap_content"/>

android:hint="姓名" android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/add_name"/>

android:orientation="horizontal">

android:text="\t帐号" android:layout_width="80dp"

android:layout_height="wrap_content"/>

android:hint="帐号" android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/add_account"/>

/res/layout/user_table.xml

encoding="utf-8"?>

android:id="@+id/relativeLayout1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android">

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_marginLeft="5dp"

android:id="@+id/userName"

android:textSize="24dip">

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_marginLeft="95dp"

android:id="@+id/userPhone"

android:textSize="24dip">

android:layout_width="wrap_content"

android:id="@+id/state1"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_alignParentRight="true"

android:layout_marginRight="50dp"

android:layout_marginLeft="20dp">

android:layout_width="wrap_content"

android:id="@+id/state2"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_alignParentRight="true"

android:layout_marginRight="5dp">

/src/MyListActivity.java

package com.liwei.happygroup;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.Context;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.BaseAdapter;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MyListActivity extends Activity {

private int[] testName = { R.string.name1,

R.string.name2,

R.string.name1,

R.string.name2, R.string.name1,

R.string.name2

};

private int[] textPhone = { R.string.phone1,

R.string.phone2,

R.string.phone3,

R.string.phone1, R.string.phone2, R.string.phone3,

R.string.phone1,

R.string.phone2, R.string.phone3 };

private

List

Object>> mData;

ArrayList

Object>> list;

public

List

Object>> myList()

{

list = new

ArrayList

Object>>();

int rowCounter =

testName.length;

for (int i = 0; i

< rowCounter; i++) {

HashMap

Object> hashMap = new HashMap

Object>();

hashMap.put("col1",

getResources().getString(testName[i]));

hashMap.put("col2",

getResources().getString(textPhone[i]));

list.add(hashMap);

}

SortList sl =

new SortList("col1",list);

sl.sortList();

return list;

}

private ListView listView;

private Button addBtn;

MyAdapter ma;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mData = myList();

ma = new MyAdapter(this);

listView = (ListView)

findViewById(R.id.listView01);

addBtn =

(Button)findViewById(R.id.addUserBtn);

listView.setAdapter(ma);

addBtn.setOnClickListener(new

OnClickListener() {

public void

onClick(View v) {

//

TODO Auto-generated method stub

addDialog();

Toast.makeText(getApplicationContext(),

"add user", Toast.LENGTH_SHORT).show();

}

});

}

public class SortList{

private Object obj;

private

ArrayList

Object>> list;

public SortList(Object o,ArrayList

Object>> list) {

// TODO

Auto-generated constructor stub

this.obj = o;

this.list = list;

}

protected void

sortList(){

if (!list.isEmpty())

{ Collections.sort(list, new

Comparator

Object>>(){

@Override

public int compare(Map

Object> object1,Map

Object> object2) {

//根据文本排序

return ((String) object1.get(obj)).compareTo((String)

object2.get(obj));

} }); }

}

}

public final class ViewHolder{

public

TextView u_name;

public TextView

u_account;

public

CheckBox u_cb1;

public

CheckBox u_cb2;

}

public class MyAdapter extends BaseAdapter{

private

LayoutInflater mInflater;

public MyAdapter(Context

context){

this.mInflater

= LayoutInflater.from(context);

}

@Override

public int getCount() {

// TODO Auto-generated method

stub

return

mData.size();

}

@Override

public Object getItem(int arg0)

{

// TODO Auto-generated method

stub

return

null;

}

@Override

public long getItemId(int arg0)

{

// TODO Auto-generated method

stub

return

0;

}

@Override

public View getView(final int

position, View convertView, ViewGroup parent) {

ViewHolder

holder = null;

if

(convertView == null) {

holder=new

ViewHolder();

convertView

= mInflater.inflate(R.layout.user_table, null);

holder.u_name

= (TextView)convertView.findViewById(R.id.userName);

holder.u_account

= (TextView)convertView.findViewById(R.id.userPhone);

holder.u_cb1

= (CheckBox)convertView.findViewById(R.id.state1);

holder.u_cb2

= (CheckBox)convertView.findViewById(R.id.state2);

convertView.setTag(holder);

}else {

holder =

(ViewHolder)convertView.getTag();

}

holder.u_name.setText((String)mData.get(position).get("col1"));

holder.u_account.setText((String)mData.get(position).get("col2"));

convertView.setOnLongClickListener(new

View.OnLongClickListener() {

@Override

public

boolean onLongClick(View v) {

//

TODO Auto-generated method stub

Toast.makeText(getApplicationContext(),

"good", Toast.LENGTH_SHORT).show();

delDialog(position);

return

false;

}

});

return convertView;

}

}

protected void delDialog(final

int position){

Builder dialog = new

AlertDialog.Builder(this);

dialog.setTitle("删除用户信息")

.setIcon(R.drawable.warning)

.setMessage("确认是否删除?")

.setPositiveButton("确定", new

DialogInterface.OnClickListener() {//确认删除操作

@Override

public

void onClick(DialogInterface dialog, int which) {

//

TODO Auto-generated method stub

//删除操作

try{

mData.remove(position);

ma.notifyDataSetChanged();

}catch(Exception e){

e.printStackTrace();

}finally{

dialog.dismiss(); //释放对话框资源

}

}

})

.setNegativeButton("取消", new

DialogInterface.OnClickListener() {

@Override

public

void onClick(DialogInterface dialog, int which) { //取消删除操作

//

TODO Auto-generated method stub

你可能感兴趣的:(android,listview高级)