Android hashmap在simpleadapter中工作,但在自定义数组适配器中不工作
下面代码的Hashmap适用于simpleadapter数组,但不适用于自定义数组适配器。为什么?我该如何修复它?请帮助。无法解决。已尝试所有操作 我正在使用lazyLoading类来加载图像Android hashmap在simpleadapter中工作,但在自定义数组适配器中不工作,android,listview,hashmap,lazy-loading,android-arrayadapter,Android,Listview,Hashmap,Lazy Loading,Android Arrayadapter,下面代码的Hashmap适用于simpleadapter数组,但不适用于自定义数组适配器。为什么?我该如何修复它?请帮助。无法解决。已尝试所有操作 我正在使用lazyLoading类来加载图像 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { HashMap<String, String&
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> o = (HashMap<String, String>) list.getItemAtPosition(position);
Toast.makeText(CustomizedListView.this, "ID '" + o.get("KEY_TITLE") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
LazyAdapter.java
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String, String>>();
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public static HashMap<String, String> song;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
公共类LazyAdapter扩展了BaseAdapter{
私人活动;
private ArrayList data=new ArrayList();
专用静态充气机=空;
公共图像加载器;
公共静态哈希映射;
公共LazyAdapter(活动a,ArrayList d){
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
imageLoader=新的imageLoader(activity.getApplicationContext());
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(R.layout.list_行,空);
TextView title=(TextView)vi.findviewbyd(R.id.title);//title
TextView艺术家=(TextView)vi.findviewbyd(R.id.artist);//艺术家名称
TextView duration=(TextView)vi.findviewbyd(R.id.duration);//duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);//thumb image
song=新HashMap();
宋=数据。获取(位置);
//在listview中设置所有值
title.setText(song.get(CustomizedListView.KEY_title));
artist.setText(song.get(CustomizedListView.KEY_-artist));
duration.setText(song.get(CustomizedListView.KEY_duration));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY\u THUMB\u URL)、THUMB\u image);
返回vi;
}
}
在自定义适配器中,您返回的是位置而不是项目
public Object getItem(int position) {
return position;
}
试试这个
public Object getItem(int position) {
return data.get(position);
}
这将解决您的问题发布带有撞车stacktrace的logcat。Hello先生在logcat中发布了错误,该错误在REDTha中,感谢您David探究该问题。请让我知道代码错误在哪里?发布您的自定义适配器代码。抱歉,我不知道
LazyAdapter
正在做什么。当您调用getItemAtPosition()
而不是您期望的HashMap
时,它似乎返回了一个Integer
。谢谢您,先生,您太棒了!!。成功了。我是否应该对公共长getItemId(int position){return position;}@JamesPatrick No执行相同的操作?它应该返回position。。保持原样基本上是只重新调整位置(即int)不是随附的数据,而是您提供的代码解决了这个问题。我正确吗,先生。真棒先生,我将继续上面的代码,并在新的活动中显示它。如果我卡住了,我将像今天一样在新的问题中发布代码,并将在此处添加注释,让您知道。再次感谢您,先生。祝您愉快。我将回到编码。
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data=new ArrayList<HashMap<String, String>>();
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public static HashMap<String, String> song;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
}
public Object getItem(int position) {
return position;
}
public Object getItem(int position) {
return data.get(position);
}