EditAdapter.java页面
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
public
class EditAdapter
extends BaseAdapter {
//
定义一个LayoutInflater来导入资源文件用
LayoutInflater inflater;
//
用来接收要绑定的数据
List<HashMap<String, Object>> aList;
//
要绑定的资源文件id
int resID;
private
int temp = -1;
Activity activity;
HashMap<String,Boolean> states=
new HashMap<String,Boolean>();
public EditAdapter(Context context, List<HashMap<String, Object>> aList,
int resId) {
this.inflater = LayoutInflater.from(context);
this.aList = aList;
this.resID = resId;
activity= (Activity)context;
}
//
绑定的数据的长度,也就是ListView项的个数
@Override
public
int getCount() {
//
TODO Auto-generated method stub
return
this.aList.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(
final
int position, View convertView, ViewGroup parent) {
Eholder holder;
if (convertView ==
null) {
convertView = inflater.inflate(R.layout.editpersonitem,
null);
holder =
new Eholder();
//
setTag方法用来设置与视图关联的标签,我的理解就是把和它相关的ViewHolder存储起来,到用的时候再拿出来
convertView.setTag(holder);
}
else {
//
如果不为null就直接通过getTag取出来
holder = (Eholder) convertView.getTag();
}
holder.itemview = (ImageView) convertView.findViewById(R.id.imgperson);
final RadioButton radio=(RadioButton) convertView.findViewById(R.id.rdoperdson);
holder.radiobutton = radio;
//
人物照片
//
图片网络路径
String imageUrl =
this.aList.get(position).get("PhotoUrl").toString();
//
图片本地路径
String imageName =
this.aList.get(position).get("PhotoName").toString();
//
缩略图在本地的路径
Bitmap imageBitmap = Utility.getLoacalBitmap(imageName);
//
如果本地有保存
if (imageBitmap !=
null) {
holder.itemview.setImageBitmap(imageBitmap);
}
else {
//
如果本地没有保存
imageBitmap = Utility.getNetBitmap(imageUrl);
if (imageBitmap !=
null) {
Utility.SaveImage(imageBitmap, imageName);
holder.itemview.setImageBitmap(imageBitmap);
}
}
//
RadioButton
holder.radiobutton.setOnClickListener(
new View.OnClickListener() {
public
void onClick(View v) {
//
TODO Auto-generated method stub
Toast.makeText(activity.getApplicationContext(), "选中的是第"+position+"条记录", 0).show();
//
重置,确保最多只有一项被选中
for(String key:states.keySet()){
states.put(key,
false);
}
states.put(String.valueOf(position), radio.isChecked());
EditAdapter.
this.notifyDataSetChanged();
}
});
boolean res=
false;
if(states.get(String.valueOf(position)) ==
null || states.get(String.valueOf(position))==
false){
res=
false;
states.put(String.valueOf(position),
false);
}
else
res=
true;
holder.radiobutton.setChecked(res);
return convertView;
}
/**
* 添加数据列表项
*
*
@param
newsitem
*/
public
void addNewsItem(List<HashMap<String, Object>> newItem) {
for (
int i = 0; i < newItem.size(); i++) {
aList.add(newItem.get(i));
}
}
}
EditPerson.java页面
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
public
class EditPerson
extends Activity
implements OnScrollListener {
private EditAdapter adapter =
null;
private
int visibleLastIndex = 0;
//
最后的可视项索引
private
int visibleItemCount;
//
当前窗口可见项总数
private
int datasize = 0;
//
数据集的条数
private Boolean isOver =
false;
private Handler handler =
new Handler();
private ListView lv;
private ArrayList<String> imageNames =
new ArrayList<String>();
private String useridstring;
private String imagePath = Environment.getExternalStorageDirectory()
+ "/DCIM/Camera/";
private String photoDate;
//
private ProgressDialog progressDialog = null;
List<HashMap<String, Object>> aList =
null;
private ProgressBar circleProgressBar;
//
弹出窗口
AlertDialog.Builder dialog =
null;
//
每滚动一页显示多少条数据
private String pageSize = "5";
//
第几页
private
int pageIndex = 1;
public
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editperson);
//
"上一步"按钮
Button btnBack = (Button) findViewById(R.id.btnBack);
//
"新增"按钮
Button btnAdd = (Button) findViewById(R.id.btnAdd);
//
"覆盖"按钮
Button btnRe = (Button) findViewById(R.id.btnRe);
//
点"上一步"按钮跳转到之前有搜索数据的搜索页面,不重新加载,并销毁此页
btnBack.setOnClickListener(
new View.OnClickListener() {
public
void onClick(View v) {
finish();
}
});
dialog =
new AlertDialog.Builder(
this);
//
获得前面的activity传来的参数
Bundle bundle =
new Bundle();
bundle =
this.getIntent().getExtras();
//
userid的字符串
useridstring = bundle.getString("Userid");
//
照片Base64的字符串
photoDate = bundle.getString("photoString");
//
操作ProgressBar控件
circleProgressBar = (ProgressBar) findViewById(R.id.circleProgressBar);
circleProgressBar.setVisibility(View.VISIBLE);
circleProgressBar.setProgress(0);
lv = (ListView) findViewById(R.id.lvPerson);
//
启动一个后台线程
handler.postDelayed(
new Runnable() {
@Override
public
void run() {
aList = getData("1",pageSize,useridstring);
if (aList ==
null) {
//
提示没有数据
dialog.setTitle("提示");
dialog.setMessage("无相关照片!");
dialog.setPositiveButton("确定",
null);
dialog.show();
}
else {
//
这里下载数据
adapter =
new EditAdapter(EditPerson.
this, aList,
R.layout.recognitionitem);
lv.setAdapter(adapter);
}
circleProgressBar.setVisibility(View.GONE);
}
}, 2000);
lv.setOnScrollListener(
this);
//
点"新增"按钮,存储图片到云端,保存到数据库
btnAdd.setOnClickListener(
new View.OnClickListener() {
public
void onClick(View v) {
//
保存用户信息
if (!IsSave()) {
return;
}
dialog.setTitle("提示");
dialog.setMessage("新增成功!");
dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog,
int which) {
Intent intent =
new Intent(EditPerson.
this,
Camera.
class);
startActivity(intent);
}
});
dialog.show();
}
});
//
点"覆盖"按钮,先删除本地手机上缓存里的照片,存储图片到云端,保存缩略图到数据库
btnRe.setOnClickListener(
new View.OnClickListener() {
public
void onClick(View v) {
//
通过RADIOBUTTON按钮取得图片名称
String strPicName="";
for (
int i = 0, j = lv.getCount(); i < j; i++) {
/*
View child = lv.getChildAt(i);
RadioButton rdobtn = (RadioButton) child.findViewById(R.id.rdoperdson);
if (rdobtn.isChecked()) {
strPicName = aList.get(i).get("PicName").toString();
}
*/
if (adapter.states.get(String.valueOf(i))==
true)
{
strPicName = aList.get(i).get("PicName").toString();
}
}
if(strPicName=="")
{
dialog.setTitle("提示");
dialog.setMessage("请选择");
dialog.setPositiveButton("确定",
null);
dialog.show();
return;
}
if(!IsCover(strPicName)){
return;
}
dialog.setTitle("提示");
dialog.setMessage("覆盖成功!");
dialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog,
int which) {
Intent intent =
new Intent(EditPerson.
this,
Camera.
class);
startActivity(intent);
}
});
dialog.show();
}
});
}
//
保存图片
private
boolean IsSave() {
//
TODO Auto-generated method stub
//
progressDialog = ProgressDialog.show(SaveUserInfo.this, "请稍等...", "获取数据中...", true);
CallWS callWS =
new CallWS();
String saveResult = callWS.AddUserPic(photoDate, useridstring);
if (saveResult.equals("-1")) {
//
progressDialog.dismiss();
Alert("保存失败!");
return
false;
}
//
progressDialog.dismiss();
return
true;
}
//
覆盖图片
private
boolean IsCover(String strPicName) {
//
TODO Auto-generated method stub
//
progressDialog = ProgressDialog.show(SaveUserInfo.this, "请稍等...", "获取数据中...", true);
//
先删除本地手机上缓存里的照片
//
缩略图在本地的路径
File file =
new File(imagePath+strPicName+ "_thumbnail.jpg");
//
原始图在本地路径
File filenew =
new File(imagePath+strPicName+ ".jpg");
//
如果本地有保存,删除
if (file !=
null) {
file.delete();
}
//
如果本地有保存原始图,删除
if (filenew !=
null) {
filenew.delete();
}
CallWS callWS =
new CallWS();
String saveResult = callWS.CoverUserPic(photoDate, useridstring,strPicName);
if (saveResult.equals("-1")) {
//
progressDialog.dismiss();
Alert("保存失败!");
return
false;
}
//
progressDialog.dismiss();
return
true;
}
//
定义一个生成数据的方法,因为HashMap的键对应的值可能是String或者是资源的ID,所以这里用的是HashMap<String,
//
Object>
private List<HashMap<String, Object>> getData(String pageIndex, String pageSize, String useridstring) {
try {
List<HashMap<String, Object>> aList =
new ArrayList<HashMap<String, Object>>();
//
获得识别的图片列表
CallWS callWS =
new CallWS();
String photoInfos = callWS.GetPhotoListByUserid(pageIndex,pageSize,useridstring);
//
String photoInfos=
//
"{\"code\":1,\"totalNum\":1,\"data\":[{\"URL\":\"
http://192.169.0.8/webServices1/Photo//03521501-EA4D-4962-B12F-618A3A39F43B_3.jpg
\",\"face\":\"\",\"imageName\":\"240E7856-C2F7-4381-B9B1-AE8F3A787890_1\",\"similiar\":0.8389}]}";
if (photoInfos==
null || photoInfos.length() == 0) {
return
null;
}
JSONObject obj =
new JSONObject(photoInfos);
String returnCode = obj.get("code").toString();
if (returnCode.equals("-1")) {
return
null;
}
datasize = Integer.parseInt(obj.get("total").toString());
JSONArray numberList = obj.getJSONArray("data");
for (
int i = 0; i < numberList.length(); i++) {
//
System.out.println(numberList.getJSONObject(i).getString("name"));
HashMap<String, Object> map =
new HashMap<String, Object>();
String PhotoName = numberList.getJSONObject(i).getString(
"PhotoName");
String PhotoUrl = numberList.getJSONObject(i).getString(
"PhotoUrl");
String strid = numberList.getJSONObject(i).getString("ID");
map.put("PhotoUrl", PhotoUrl);
map.put("id", strid);
map.put("PhotoName", imagePath+PhotoName+ "_thumbnail.jpg");
map.put("PicName", PhotoName);
aList.add(map);
imageNames.add(numberList.getJSONObject(i).getString(
"PhotoName"));
}
return aList;
}
catch (Exception e) {
return
null;
}
}
@Override
public
void onScroll(AbsListView view,
int firstVisibleItem,
int visibleItemCounts,
int totalItemCount) {
//
TODO Auto-generated method stub
visibleItemCount = visibleItemCounts;
visibleLastIndex = firstVisibleItem + visibleItemCount;
//
如果所有的记录选项等于数据集的条数,则移除列表底部视图
if (totalItemCount >= datasize&& datasize > 0) {
/*
* listView.removeFooterView(loadMoreView); Toast.makeText(this,
* "数据全部加载完!", Toast.LENGTH_LONG).show();
*/
isOver =
true;
}
}
@Override
public
void onScrollStateChanged(AbsListView view,
int scrollState) {
//
数据集最后一项的索引
int itemsLastIndex = adapter.getCount() - 1;
int lastIndex = itemsLastIndex + 1;
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& visibleLastIndex == lastIndex && !isOver) {
//
如果是自动加载,可以在这里放置异步加载数据的代码
//
lv.addFooterView(btnMore);
//
设置列表底部视图
//
btnMore.setText("正在加载中...");
//
设置按钮文字
circleProgressBar.setVisibility(View.VISIBLE);
circleProgressBar.setProgress(0);
handler.postDelayed(
new Runnable() {
@Override
public
void run() {
loadMoreData();
adapter.notifyDataSetChanged();
circleProgressBar.setVisibility(View.GONE);
//
btnMore.setText("查看更多...");
//
恢复按钮文字
//
lv.removeFooterView(btnMore);
}
}, 2000);
}
}
private
void Alert(String info) {
dialog.setTitle("提示");
dialog.setMessage(info);
dialog.setPositiveButton("确定",
null);
dialog.show();
}
/**
* 加载更多数据
*/
private
boolean loadMoreData() {
//
获得当前页面的页数
pageIndex++;
List<HashMap<String, Object>> aList = getData(Integer.toString(pageIndex),
pageSize, useridstring);
if (aList ==
null) {
pageIndex--;
return
false;
}
adapter.addNewsItem(aList);
return
true;
}
}
XML页面
editperson.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ProgressBar android:layout_width="wrap_content"
android:id="@+id/circleProgressBar" android:layout_height="wrap_content"></ProgressBar>
<ListView
android:id="@+id/lvPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/linearLayout1"
android:layout_alignParentTop="true">
</ListView>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/btnBack"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="上一步" />
<Button
android:id="@+id/btnAdd"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="新增" />
<Button
android:id="@+id/btnRe"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:text="覆盖" />
</LinearLayout>
</RelativeLayout>
editpersonitem.xml页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgperson"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dip" />
<RadioButton
android:id="@+id/rdoperdson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dip"/>
</LinearLayout>
</RelativeLayout>
已经调试通过,避免空指针
java.lang.NullPointerException 错误 ,网上之前找的给Radiobutton 设置ID为position的方法,两种都尝试过,都会再不同情况出现空指针错误