来自url的文本和图像在ListView中出错
我正在尝试用文本和图像创建Listview。图像是从URL显示的。 我从数据库中提取文本和图像url,并将其存储在ArrayList来自url的文本和图像在ListView中出错,listview,adapter,Listview,Adapter,我正在尝试用文本和图像创建Listview。图像是从URL显示的。 我从数据库中提取文本和图像url,并将其存储在ArrayList。 低于我的代码,但不工作 适配器出现问题,但我找不到问题。 有人能帮我吗? 谢谢 活动: public class Test extends Activity { ListView list; LazyAdapter adapter; String result; ArrayList<HashMap<String, String>> li
低于我的代码,但不工作 适配器出现问题,但我找不到问题。
有人能帮我吗?
谢谢 活动:
public class Test extends Activity {
ListView list;
LazyAdapter adapter;
String result;
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
list=(ListView)findViewById(R.id.list);
try{
DBConnection DBC = new DBConnection();
String URL = "http://10.0.2.2/Android.php";
DBC.DBConnection2(URL);
result = DBC.dd.getResult();
}catch (Exception e) {
Toast.makeText(getApplicationContext(), "Erreur BD access", Toast.LENGTH_LONG).show();
}
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int j=0;j<jArray.length();j++){
json_data = jArray.getJSONObject(j);
String name = json_data.getString("Name");
String title = json_data.getString("Title");
String logo = json_data.getString("Logo");
map = new HashMap<String, String>();
map.put("name", name);
map.put("title", title);
map.put("img", logo);
listItem.add(map);
}
}catch (JSONException e1) {
e1.printStackTrace();
}
adapter=new LazyAdapter(this, listItem);
list.setAdapter(adapter);
}
}
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> list) {
activity = a;
data=list;
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.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
try {
HashMap<String, String> ma = data.get(position);
for(Entry<String, String> mapEntry: ma.entrySet()) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
if (key.equals("name")) text.setText(value);
if (key.equals("img")) imageLoader.DisplayImage(value, image);
}
}catch (Exception e){
e.printStackTrace();
}
return vi;
}
}
此处指定的行:
Caused by: java.lang.NullPointerException at com.fedorvlasov.lazylist.Test.onCreate(Test.java:84)
是
list.setAdapter(适配器)
我的最佳猜测是,id为list
的ListView在您的test.xml
布局中不存在。您可以发布test.xml
的内容吗?您是对的,这就是问题所在,我在活动中加载了错误的布局文件。非常感谢。请务必花时间将问题标记为已解决,然后:)
Caused by: java.lang.NullPointerException at com.fedorvlasov.lazylist.Test.onCreate(Test.java:84)