如何将图像添加到孩子的可扩展列表视图?

时间:2014-03-20 09:30:00

标签: android imageview expandablelistview

我的应用程序使用ExpandableListView,其中包含4个父组和子组。

子行包括ImageView和文本(标志和名称)

到目前为止,我已经使用ExpandableListView为所有孩子的子行创建了相同的图像。

我希望每行ImageViewTextView

我在drawable文件夹中有图像如何在ExpandableListView中使用它。

这是ExpandableList活动的代码,所有chlidren行都有相同的图像。

MyExpandableAdapter

package com.devleb.expendablelistdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;

public class MyExpandableAdapter extends BaseExpandableListAdapter {

    private Activity activity;
    private ArrayList<Object> childtems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;

    // constructor
    public MyExpandableAdapter(ArrayList<String> parents,
            ArrayList<Object> childern) {
        this.parentItems = parents;
        this.childtems = childern;
    }

    public void setInflater(LayoutInflater inflater, Activity activity) {
        this.inflater = inflater;
        this.activity = activity;
    }

    // method getChildView is called automatically for each child view.
    // Implement this method as per your requirement

    // method getGroupView is called automatically for each parent item
    // Implement this method as per your requirement

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return ((ArrayList<String>) childtems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

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

    @Override
    public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
    }

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

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

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        child = (ArrayList<String>) childtems.get(groupPosition);

        TextView textView = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child_view, null);
        }

        // get the textView reference and set the value
        textView = (TextView) convertView.findViewById(R.id.textViewChild);
        textView.setText(child.get(childPosition));

        // set the ClickListener to handle the click event on child item
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                Toast.makeText(activity, child.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;

    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.parent_view, null);
        }

        ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);

        return convertView;
    }

}

ExpandableListMainActivity

package com.devleb.expendablelistdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;

public class ExpandableListMainActivity extends ExpandableListActivity {
    // Create ArrayList to hold parent Items and Child Items
    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();

    public HashMap<Integer, List<Integer>> childImages;


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Create Expandable List and set it's properties
        ExpandableListView expandableList = getExpandableListView();
        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        // Set the Items of Parent
        setGroupParents();
        // Set The Child Data
        setChildData();

        // Create the Adapter
        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems,
                childItems);

        adapter.setInflater(
                (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
                this);

        // Set the Adapter to expandableList
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);
    }

    // method to add parent Items
    public void setGroupParents() {
        parentItems.add("Africa");
        parentItems.add("Asia");
        parentItems.add("Europe");
        parentItems.add("North and Central America");
        parentItems.add("South America");
    }

    // method to set child data of each parent
    public void setChildData() {

        // Add Child Items for Fruits
        ArrayList<String> child = new ArrayList<String>();
        child.add("Algeria");
        child.add("Côte d'Ivoire");
        child.add("Côte d'Ivoire");
        child.add("Cameroon");

        childItems.add(child);

        // Add Child Items for Flowers
        child = new ArrayList<String>();
        child.add("Australia");
        child.add("Japan");
        child.add("Iran");
        child.add("Korea Republic");

        childItems.add(child);

        // Add Child Items for Animals
        child = new ArrayList<String>();
        child.add("Belgium");
        child.add("Croatia");
        child.add("France");
        child.add("Greece");
        child.add("Netherlands");
        child.add("Russia");
        child.add("Switzerland");
        child.add("Bosnia and Herzegovina");
        child.add("England");
        child.add("Germany");
        child.add("Italy");
        child.add("Portugal");
        child.add("Spain");

        childItems.add(child);

        // Add Child Items for Birds
        child = new ArrayList<String>();
        child.add("Costa Rica");
        child.add(" Mexico");
        child.add(" Honduras");
        child.add("USA");

        childItems.add(child);

        // Add Child Items for Birds
        child = new ArrayList<String>();
        child.add("Argentina");
        child.add("Chile");
        child.add("Ecuador");
        child.add("Brazil");
        child.add("Colombia");
        child.add("Uruguay");

        childItems.add(child);

    }

}

0 个答案:

没有答案