1

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

适配器的内容发生了变化,但 ListView 没有收到通知。确保适配器的内容不是从后台线程修改的,而只是从 UI 线程修改的。[在 ListView(16908298, class android.widget.ListView) with Adapter(class 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;
    }
4

2 回答 2

1

尝试在将项目添加到列表时调用 notifyDataSetChanged(),requestLayout() 也可以解决问题。

于 2012-07-02T12:43:47.627 回答
0

你做错了首先从webservices获取所有值然后把它给适配器ok

于 2012-07-02T13:09:00.997 回答