我在使用ArrayList<HashMap<String, Object>>的时候,不知道怎么获取里面的值。
想要获取某个位置的值,为什么下面的方法不可以?
private ArrayList<HashMap<String, Object>> resultList;
String name=(String)resultList.get(position).get("salename");
报错:
threadid=1: thread exiting with uncaught exception (group=0x40015560)
package com.flea;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
public final class ViewHolder {
public TextView txtname;
public TextView txttime;
public TextView txtmoney;
}
private LayoutInflater mInflater;
private ArrayList<HashMap<String, Object>> resultList;
public MyAdapter(Context context,ArrayList<HashMap<String, Object>> resultList) {
this.mInflater = LayoutInflater.from(context);
this.resultList=resultList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return resultList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return resultList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (convertView==null) {
holder=new ViewHolder();
convertView=mInflater.inflate(R.layout.list_menu,null);
Log.d("sqt", "positon " + position + "converView is null,new "+ convertView);
holder.txtname = (TextView) convertView.findViewById(R.id.txtname);
holder.txttime = (TextView) convertView.findViewById(R.id.txttime);
holder.txtmoney = (TextView) convertView.findViewById(R.id.txtmoney);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
Log.d("sqt", "positon:" + position + "converView is not null,"
+ convertView);
}
Log.d("sqt", "resultList:"+resultList) ;
String name=(String)resultList.get(position).get("salename");
System.out.println(name);
//holder.txtname.setText((String)resultList.get(position).get("salename"));
//holder.txttime.setText((String)resultList.get(position).get("saletime"));
//holder.txtmoney.setText((String)resultList.get(position).get("salemoney"));
return convertView;
}
}
------解决思路----------------------
String name=(String)resultList.get(position).get("salename");有可能是强转错误。
ArrayList<HashMap<String, Object>> resultList;
HashMap<String, Object> map = resultList.get(0);
Object obj = map.get("xxx");
这个obj类型是不确定的。
------解决思路----------------------
用resultList.get(position).get("salename").toString();试试。