在ListView崩溃程序中选择项目

时间:2012-07-02 12:39:37

标签: android mysql listview

我有一个ListView,它允许我能够选择我喜欢的图片,它工作得很好,但问题是当我选择其他图片它粉碎时,我得到这个错误信息,我不明白和这些图片来自MySql数据库。

  

适配器的内容已更改,但ListView未收到   通知。确保未修改适配器的内容   来自后台线程,但仅来自UI线程。 [在   带适配器的ListView(16908298,类android.widget.ListView)(类   car.store.carstore.MobileArrayAdapter)]

提前完成。

MyAdapter看起来像这样

public class MobileArrayAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] MyCarList = null;
    private ArrayList<String> MyCar = new ArrayList<String>();
    private ArrayList<String> CarImageUrl = new ArrayList<String>();
     //ArrayList<HashMap<String, String>> MyCarList = new ArrayList<HashMap<String, String>>();
     //ArrayList<HashMap<String, String>> CarImageUrl = new ArrayList<HashMap<String, String>>();

    public MobileArrayAdapter(Context context,ArrayList<String> Bname,ArrayList<String> BUrl) {
        super(context, R.layout.list_mobile, Bname);
        this.context = context;
        this.MyCar = Bname;
        this.CarImageUrl = BUrl;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_mobile, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
        textView.setText(MyCar.get(position));
        Bitmap bitmap = null;
        // Change icon based on name
        JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/car.php");
           // http://api.geonames.org/earthquakesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&username=demo  
            try{                
                JSONArray  earthquakes = json.getJSONArray("cars");

                //for(position=0;position<earthquakes.length();position++){
                    JSONObject e = earthquakes.getJSONObject(position);
                    String BB = e.getString("carname");
                    MyCar.add(BB);

                    String UU = e.getString("carimage");
                    CarImageUrl.add(UU);

                //}

            }catch(JSONException e){
                 Log.e("log_tag", "Error parsing data "+e.toString());
            }


            String s = MyCar.get(position);
            String i = CarImageUrl.get(position);

            System.out.println(s);
            System.out.println(i);


            try {
                bitmap = BitmapFactory.decodeStream((InputStream)new URL(i).getContent());
            } catch (MalformedURLException err) {
                // TODO Auto-generated catch block
                err.printStackTrace();
            } catch (IOException err) {
                // TODO Auto-generated catch block
                err.printStackTrace();
            }

            //if (!s.equals("")) {
                imageView.setImageBitmap(bitmap);
            //} else {
            System.out.println("Bitmap image: "+position+"="+bitmap);
                //imageView.setImageResource(R.drawable.android_logo);
            //}


        return rowView;
    }

2 个答案:

答案 0 :(得分:1)

在向列表中添加项目时尝试调用notifyDataSetChanged(),requestLayout()也可以执行此操作。

答案 1 :(得分:0)

你做错了先从webservices获取所有值,然后将其交给适配器确定