图像显示在android中的listview的第一行

时间:2014-04-22 07:58:13

标签: android listview

我需要从服务器显示图像。我下载图像表单服务器。图像在列表视图行中从服务器显示。

问题是 -

1)图像仅显示在列表视图的第一行,第一行的图像连续变化。但是一段时间后其他行也有图像,但方式错误(第一行不包含图像网址,但在列表视图中显示图像)。

1。来自服务器的GetCategory详细信息

 private class GetCategoryFromServer extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return JsonParser.Webserice_Call_URL(ConstantsUrl.Get_All_category,
                null, null);
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        // System.out.println("result from response " + result);
        try {
            parseResponse(result);

        } catch (Exception e) {
            e.printStackTrace();
        }
        categoryAdapter.notifyDataSetChanged();

    }
}

2。解析结果&amp;存储在ArrayList

  private void parseResponse(String result) throws Exception {
    categoryList.clear();

    JSONArray jsonArrayMain = new JSONArray(result);
    for (int i = 0; i < jsonArrayMain.length(); i++) {
        JSONObject categoryJson = jsonArrayMain.getJSONObject(i);
        CategoryMetadata metadataObj = new CategoryMetadata();
        metadataObj.setName(JsonParser
                .getkeyValue_Str(categoryJson, "name"));
        metadataObj.setId(JsonParser.getkeyValue_Str(categoryJson, "id"));
        metadataObj.setUrlKey(JsonParser.getkeyValue_Str(categoryJson,
                "urlkey"));
        metadataObj.setDesc(JsonParser
                .getkeyValue_Str(categoryJson, "desc"));
        metadataObj.setImage(JsonParser.getkeyValue_Str(categoryJson,
                "image"));
        metadataObj.setParentId(JsonParser.getkeyValue_Str(categoryJson,
                "parent_id"));
        categoryList.add(metadataObj);
    }
    // System.out.println("size of arrayList" + categoryList.size());

}

** 3。适配器类**

      private class CategoryAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    public CategoryAdapter() {
        mInflater = LayoutInflater.from(getActivity());

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return categoryList.size();
    }

    @Override
    public CategoryMetadata getItem(int arg0) {
        // TODO Auto-generated method stub
        return categoryList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder holderObj = null;
        if (convertView == null) {
            holderObj = new ViewHolder();
            convertView = mInflater.inflate(R.layout.row_category, null);
            holderObj.categoryTitle = (TextView) convertView
                    .findViewById(R.id.title);
            holderObj.background = (LinearLayout) convertView
                    .findViewById(R.id.background);
            convertView.setTag(holderObj);
        } else {
            holderObj = (ViewHolder) convertView.getTag();

        }
        CategoryMetadata value = getItem(position);
        holderObj.categoryTitle.setText(value.getName());
        try {
            String _url = value.getImage().toString();
            if (_url != null && !_url.equals("")) {

                new ImageDownloaderTask(holderObj.background).execute(value
                        .getImage());

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

}

private class ViewHolder {
    private LinearLayout background;
    private TextView categoryTitle;

}

4.下载图片

private class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {

    private final LinearLayout imageViewReference;

    public ImageDownloaderTask(LinearLayout imageView) {
        // TODO Auto-generated constructor stub
                    imageViewReference = imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub
        return AppUtiles.download(params[0], getActivity());
    }

    @SuppressLint("NewApi") @Override
    protected void onPostExecute(Bitmap bitmap) {
        // TODO Auto-generated method stub
        super.onPostExecute(bitmap);
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null && isAdded()) {
            // LinearLayout imageView = (LinearLayout) imageViewReference
            // .get();
            // if (imageView != null&&isAdded()) {

            /*
             * if (bitmap != null) { imageView.setImageBitmap(bitmap); }
             * else { imageView.setImageDrawable(imageView.getContext().
             * getResources() .getDrawable(R.drawable.list_placeholder)); }
             */
            Drawable d = new BitmapDrawable(getResources(), bitmap);
            if (bitmap != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    imageViewReference.setBackground(d);
                    ;
                } else {
                                     imageViewReference.setBackgroundDrawable(d);
                }
            }
            categoryAdapter.notifyDataSetChanged();
            // }

        }

    }
}

1 个答案:

答案 0 :(得分:0)