布局用的是RelativeLayout这种布局方式,要指定位置,不太好,以后如果有其它好的方式再修改

main.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/MyListView">
</ListView>
</LinearLayout>


list_items.xml布局文件:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/myListItem"
android:paddingBottom="5dip" android:paddingLeft="12dip"
android:paddingRight="12dip">
<ImageView android:id="@+id/img" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"></ImageView>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/itemTitle"
android:textSize="20dip" android:paddingTop="15px"
android:layout_marginLeft="70px">
</TextView>
<CheckBox android:id="@+id/cb" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:focusable="false"></CheckBox>
</RelativeLayout>


MyList.java类:


package iaiai.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class MyList extends Activity {

private ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

list = (ListView) findViewById(R.id.MyListView);

// 配置适配器
MyCheckBoxAdapter adapter = new MyCheckBoxAdapter(this,getData()); // 布局里的控件id
// 添加并且显示
list.setAdapter(adapter);

// 添加点击
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第" + arg2 + "个项目");
Toast.makeText(MyList.this, "点击第" + arg2 + "个项目",
Toast.LENGTH_LONG).show();
}
});

// 添加长按点击
list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// menu.setHeaderIcon(R.drawable.icon); //字义图片样式
menu.setHeaderTitle("长按菜单选项:");
menu.add(1, 0, 0, "编辑");
menu.add(0, 1, 0, "删除");
}
});
}

@Override
public boolean onContextItemSelected(MenuItem item) {
int selectedPosition = ((AdapterContextMenuInfo) item.getMenuInfo()).position; // 刚才选择的
Map<String, String> map = (HashMap) list
.getItemAtPosition(selectedPosition); // ListView中的数据
Toast.makeText(MyList.this,
"你选择的:" + selectedPosition + ",内容为:" + map.get("itemTitle"),
Toast.LENGTH_LONG).show(); // 显示那条数据

String temp = "";
switch (item.getItemId()) {
case 0:
temp = "编辑";
break;
case 1:
temp = "删除";
break;
default:
break;
}
// Toast.makeText(this, temp + "處理", Toast.LENGTH_LONG).show();
return super.onContextItemSelected(item);
}

private List<Map<String, String>> getData() {
// 组织数据源
List<Map<String, String>> mylist = new ArrayList<Map<String, String>>();
for (int i = 0; i < 10; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("" + i, i + "");
map.put("itemTitle", "文章" + i);
mylist.add(map);
}
return mylist;
}

/**
* listview中点击按键弹出对话框
*/
public void showInfo() {
new AlertDialog.Builder(this).setTitle("我的listview")
.setMessage("介绍...")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
}


MyCheckBoxAdapter.java类:


package iaiai.test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyCheckBoxAdapter extends BaseAdapter {

private LayoutInflater mInflater;

private List<Map<String, String>> listData;

private Map<Integer, Map<String, String>> selectMap = new HashMap<Integer, Map<String, String>>();

private class ViewHolder {
public ImageView img;
public TextView title;
public CheckBox checkBox;
}

public MyCheckBoxAdapter(Context context, List<Map<String, String>> listData) {
this.mInflater = LayoutInflater.from(context);
this.listData = listData;
}

@Override
public int getCount() {
return listData.size();
}

@Override
public Object getItem(int position) {
return listData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@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.list_items, null);
final View view = convertView;
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.title = (TextView) convertView.findViewById(R.id.itemTitle);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.cb);
holder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (selectMap.get(position) != null) {
selectMap.remove(position);
} else {
selectMap.put(position, listData.get(position));
}
Toast.makeText(view.getContext(),
"你选择了:" + selectMap.size() + "个.",
Toast.LENGTH_LONG).show();
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.img.setBackgroundResource(R.drawable.icon);
holder.title.setText(listData.get(position).get("itemTitle"));

if (selectMap.get(position) != null) {
holder.checkBox.setChecked(true);
} else {
holder.checkBox.setChecked(false);
}

return convertView;
}
}



这篇文章主要是学习BaseAdapter类的使用,发的文章都是经过运行可以跑起来的,并且代码全会发到上面。



下面是运行结果:


[img]http://dl.iteye.com/upload/attachment/490429/554fb750-d6f4-32bd-9e26-e3f7e8af0e0e.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/490431/dfd076b9-9cfa-391b-87c0-d1adddc97981.jpg[/img]