【问题标题】:How to add image icon to expandablelistview child list如何将图像图标添加到可扩展列表视图子列表
【发布时间】:2014-01-24 21:43:41
【问题描述】:

我尝试将图像图标添加到 expandablelistview 子列表。我无法正确执行此操作。 在我的应用程序中,子列表从 API(JSON) 加载数据。

我想将图像添加到不同的数据中。例如 address.png + address 一样明智。

谁能帮我创建它?

这是我的主要活动代码。

public void ListDrwaer() {

        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();
        List<String> restData;

        try {
            JSONObject jsonResponse = new JSONObject(strJson1);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants");



            for (int i = 0; i < jsonMainNode.length(); i++) {

                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                String restName = jsonChildNode.optString("name");
                String address = "Address: "+jsonChildNode.optString("address");
                String mobile = "Contact No: "+jsonChildNode.optString("mobile");
                String direction = "Direction: "+jsonChildNode.optString("direction");
                String bestTime = "BestTime to visite: "+jsonChildNode.optString("bestTime");
                String food = "Food: "+jsonChildNode.optString("food");
                String dress = "Dress: "+jsonChildNode.optString("dress");
                String priceRange = "Price Range: "+jsonChildNode.optString("priceRange");
                //String url = jsonChildNode.optString("priceRange");

                listDataHeader.add(restName);
                restData = new ArrayList<String>();
                restData.add(address);
                restData.add(mobile);
                restData.add(direction);
                restData.add(bestTime);
                restData.add(food);
                restData.add(dress);
                restData.add(priceRange);

                listDataChild.put(restName, restData);

            }


        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), "Error..." + e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);


    }


}

这是适配器类

public class ExpandableListAdapter extends BaseExpandableListAdapter {

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

    ImageView image;

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

    Integer[] images = { R.drawable.address,
            R.drawable.clock, R.drawable.location };

    @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.lblListItem);


        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;
    }



}

这是子列表xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textColor="#daac56"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"/>

</LinearLayout>

【问题讨论】:

  • 您已经将该地址添加到您的TextViewImageView 也是如此。只需找到视图并设置您的图像。我真的不明白你的问题是什么。你能解释一下“我做错了”是什么意思吗?
  • 是的。我想添加图像视图,然后添加文本视图。我不明白如何将 imageview 添加到这个子列表中。
  • @anuruddhika 你找到解决方案了吗?我也有同样的问题。
  • @anuruddhika 如果可能的话,请指导我。

标签: android android-layout android-listview expandablelistview


【解决方案1】:

您已经在BaseExpandlableListAdapter 中通过以下行将文本添加到您的TextView

TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);    
txtListChild.setText(childText);

对你的图片做同样的事情:

ImageView imgViewChild = (ImageView ) convertView
                .findViewById(R.id.img);    
imgViewChild.setImageResource(R.drawable.address);

【讨论】:

  • 我应该添加 7 张图片。那我该怎么办??例如地址图片+地址,然后联系图标+联系号码等等..
  • 在设置图片之前先做一个 if 语句...如何确定应该显示 7 张图片中的哪一张?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
相关资源
最近更新 更多