我想在可扩展列表视图中的imageview中设置图像

时间:2015-03-24 08:32:36

标签: java android image

我有图片网址,我想在图片视图中设置图片。

我有以下代码:

ExpandableListAdapter.java

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<?> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<?> listDataHeader,
            HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.name);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);

        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

以上是我的适配器。以下是我的main.java。

           List<String> listDataHeader;
           listDataHeader = new ArrayList<String>();

                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_CID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_LOGO, logo);
                // adding HashList to ArrayList

                listDataHeader.add(name);

上面是我的代码,其中logo是图片网址。

以下是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/list_row_selector"
    android:orientation="vertical"
    android:padding="8dp" >

    <ImageView
        android:id="@+id/companythumbnail"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="5dp"
        android:scaleType="centerCrop"
        android:src="@drawable/splashlogo" />

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/companythumbnail"
        android:layout_toRightOf="@+id/companythumbnail"
        android:textColor="@color/black"
        android:textSize="@dimen/name" />

</RelativeLayout>

现在我如何使用此自定义适配器在imageview中显示图像。

我有URL格式的图片。我也尝试使用位图,但它没有用。

那我怎么能把那个网址传递到imageview。?

我对此完全感到困惑。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用Android通用图像加载程序库。非常有用。

https://github.com/nostra13/Android-Universal-Image-Loader