我使用ListView
创建了一个景点列表。程序运行并显示输出但在滚动列表时它会关闭一个力。我在这里发布我的代码。
请给我一些建议。
package com.example.attraction;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
private ListView listView;
private ArrayList<HashMap<String,Object>> data;
private ListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
data = new ArrayList<HashMap<String, Object>>();
prePareListData();
adapter = new ListAdapter(this,data);
listView.setAdapter(adapter);
}
private void prePareListData(){
HashMap<String,Object> row1 = new HashMap<String, Object>();
row1.put("ID",1);
row1.put("IMAGE",R.drawable.taj);
row1.put("NAME","Taj Mahal");
row1.put("ARROWIMAGE",R.drawable.arrow);
data.add(row1);
HashMap<String,Object> row2 = new HashMap<String, Object>();
row2.put("ID",2);
row2.put("IMAGE",R.drawable.mehtab_baug);
row2.put("NAME","Mehtab Bagh");
row2.put("ARROWIMAGE",R.drawable.arrow);
data.add(row2);
HashMap<String,Object> row3 = new HashMap<String, Object>();
row3.put("ID",3);
row3.put("IMAGE",R.drawable.ram_baugh);
row3.put("NAME","Ram bagh");
row3.put("ARROWIMAGE",R.drawable.arrow);
data.add(row3);
HashMap<String,Object> row4 = new HashMap<String, Object>();
row4.put("ID",4);
row4.put("IMAGE",R.drawable.agra_fort);
row4.put("NAME","Agra Fort");
row4.put("ARROWIMAGE",R.drawable.arrow);
data.add(row4);
HashMap<String,Object> row5 = new HashMap<String, Object>();
row4.put("ID",5);
row4.put("IMAGE",R.drawable.musamman_burj);
row4.put("NAME","Musamman Burj");
row4.put("ARROWIMAGE",R.drawable.arrow);
data.add(row5);
HashMap<String,Object> row6 = new HashMap<String, Object>();
row5.put("ID",6);
row5.put("IMAGE",R.drawable.tomb_of_akbar_the_great);
row5.put("NAME","Tomb of Akbar The Great");
row5.put("ARROWIMAGE",R.drawable.arrow);
data.add(row6);
HashMap<String,Object> row7 = new HashMap<String, Object>();
row6.put("ID",7);
row6.put("IMAGE",R.drawable.tomb_of_itmad_ud_daula);
row6.put("NAME","Tomb of Itmad Ud Daula");
row6.put("ARROWIMAGE",R.drawable.arrow);
data.add(row7);
HashMap<String,Object> row8 = new HashMap<String, Object>();
row7.put("ID",8);
row7.put("IMAGE",R.drawable.fatepur_sikri);
row7.put("NAME","Fatehpur Sikri");
row7.put("ARROWIMAGE",R.drawable.arrow);
data.add(row8);
HashMap<String,Object> row9 = new HashMap<String, Object>();
row8.put("ID",9);
row8.put("IMAGE",R.drawable.panch_mahal);
row8.put("NAME","Panch Mahal");
row8.put("ARROWIMAGE",R.drawable.arrow);
data.add(row9);
HashMap<String,Object> row10 = new HashMap<String, Object>();
row9.put("ID",10);
row9.put("IMAGE",R.drawable.jamma_masjid);
row9.put("NAME","Jamma Masjid");
row9.put("ARROWIMAGE",R.drawable.arrow);
data.add(row10);
HashMap<String,Object> row11 = new HashMap<String, Object>();
row10.put("ID",11);
row10.put("IMAGE",R.drawable.chini_ka_rauza);
row10.put("NAME","Chini ka Rauza");
row10.put("ARROWIMAGE",R.drawable.arrow);
data.add(row11);
HashMap<String,Object> row12 = new HashMap<String, Object>();
row10.put("ID",12);
row10.put("IMAGE",R.drawable.mankameshwar_temple);
row10.put("NAME","Mankameshwar Temple");
row10.put("ARROWIMAGE",R.drawable.arrow);
data.add(row12);
HashMap<String,Object> row13 = new HashMap<String, Object>();
row10.put("ID",13);
row10.put("IMAGE",R.drawable.tomb_of_mariam);
row10.put("NAME","Tomb of Mariam Zamani");
row10.put("ARROWIMAGE",R.drawable.arrow);
data.add(row13);
HashMap<String,Object> row14 = new HashMap<String, Object>();
row10.put("ID",14);
row10.put("IMAGE",R.drawable.guru_ka_tal);
row10.put("NAME","Guru ka Tal");
row10.put("ARROWIMAGE",R.drawable.arrow);
data.add(row14);
}
class ListAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String,Object>> list;
public ListAdapter(Context context,ArrayList<HashMap<String,Object>> list){
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.row,null,false);
holder.imgItemImage = (ImageView) convertView.findViewById(R.id.imgItemImage);
holder.imgItemArrow = (ImageView) convertView.findViewById(R.id.imgItemArrow);
holder.txtItemName = (TextView) convertView.findViewById(R.id.txtItemName);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.imgItemImage.setImageResource((Integer)list.get(position).get("IMAGE"));
holder.imgItemArrow.setImageResource((Integer)list.get(position).get("ARROWIMAGE"));
holder.txtItemName.setText(list.get(position).get("NAME").toString());
return convertView;
}
class ViewHolder{
ImageView imgItemImage;
ImageView imgItemArrow;
TextView txtItemName;
}
}
}
答案 0 :(得分:0)
将第一行替换为第二行:
ArrayList<HashMap<String, Object>>();
ArrayList<HashMap<String, String>>();
还有一件事:R.drawable.arrow
它不是一个字符串,它是一个整数id。
答案 1 :(得分:0)
问题在于row11
方法
row14
到prePareListData
的创建和设置数据
你在做什么
HashMap<String,Object> row11 = new HashMap<String, Object>();
row10.put("ID",11);
row10.put("IMAGE",R.drawable.chini_ka_rauza);
row10.put("NAME","Chini ka Rauza");
row10.put("ARROWIMAGE",R.drawable.arrow);
data.add(row11);
您正在创建对象row11
,但未在其中设置任何值并将其添加到data
。所以当getView
调用
holder.txtItemName.setText(list.get(position).get("NAME").toString());
list.get(position).get("NAME")
基本上是null
,您试图在toString
null String
所以你得到NullPointerException
更改
HashMap<String,Object> row11 = new HashMap<String, Object>();
row10.put("ID",11);
row10.put("IMAGE",R.drawable.chini_ka_rauza);
row10.put("NAME","Chini ka Rauza");
row10.put("ARROWIMAGE",R.drawable.arrow);
data.add(row11);
到
HashMap<String,Object> row11 = new HashMap<String, Object>();
row11.put("ID",11);
row11.put("IMAGE",R.drawable.chini_ka_rauza);
row11.put("NAME","Chini ka Rauza");
row11.put("ARROWIMAGE",R.drawable.arrow);
data.add(row11);
并对 row12 执行相同操作 row14