如何在expandablelistview中添加图像?

时间:2015-04-03 11:13:09

标签: android expandablelistview

我在我的项目中使用expandablelistview,我想为组视图添加不同的图像,并且在子视图中我想要做同样的事情,但它不起作用,以下是我的代码..

MainActivity.java

public class MainActivity extends Activity {

ExpandableTests listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;

 private List<Integer> group;

    private HashMap<Integer, List<Integer>> Child;


Expandableimageadapter imgsadadapter;


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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // get the listview
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();


    expListView.setGroupIndicator(null);

    //imgsadadapter=new Expandableimageadapter(this, groupImages, childImages);

//  listAdapter = new ExpandableTests(this, listDataHeader, group, listDataChild, Child);

    listAdapter = new ExpandableTests(this, listDataHeader, groupImages, listDataChild, childImages);

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

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                                    listDataHeader.get(groupPosition)).get(
                                    childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });
}

/*
 * Preparing the list data
 */
private void prepareListData() {



    groupImages= new ArrayList<Integer>();
    groupImages.add(R.drawable.adminsetting);
    groupImages.add(R.drawable.productcategory);
   // groupImages.add(R.drawable.drinks);
   // groupImages.add(R.drawable.deserts);

    childImages = new HashMap<Integer, List<Integer>>();
    List<Integer> Maincat = new ArrayList<Integer>();
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
    Maincat.add(R.drawable.productcategory);
   // vegetablesi.add(R.drawable.Onions);
   // vegetablesi.add(R.drawable.Garlic);

    List<Integer> subcat = new ArrayList<Integer>();
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
    subcat.add(R.drawable.productcategory);
   // fruitsi.add(R.drawable.Kiwifruit);
   // fruitsi.add(R.drawable.Grape);


   /* List<Integer> drinksi = new ArrayList<Integer>();
    drinksi.add(R.drawable.Vodka);
    drinksi.add(R.drawable.Milk);
    drinksi.add(R.drawable.Water);
    drinksi.add(R.drawable.CocaCola);
    drinksi.add(R.drawable.Sprite);


    List<Integer> desertsi = new ArrayList<Integer>();
    desertsi.add(R.drawable.Vodka);
    desertsi.add(R.drawable.Milk);
    desertsi.add(R.drawable.Water);
    desertsi.add(R.drawable.CocaCola);
    desertsi.add(R.drawable.Sprite);*/


   // childImages.put(groupImages.get(0), Maincat);
  //  childImages.put(groupImages.get(1), subcat);
    childImages.put(0, Maincat);
    childImages.put(1, subcat);


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

    // Adding child data
    listDataHeader.add("Admin Setting");
    listDataHeader.add("Product & Category");
    listDataHeader.add("Client & Orders");

    // Adding child data
    List<String> top250 = new ArrayList<String>();
    top250.add("State City");
    top250.add("Sales Person");
    top250.add("City Sales Person");
    top250.add("Product Boxtype");
    top250.add("Shipping Carrier");
    //top250.add("The Dark Knight");
//  top250.add("12 Angry Men");

    List<String> nowShowing = new ArrayList<String>();
    nowShowing.add("Product");
    nowShowing.add("Category");
    nowShowing.add("Category Products");
    nowShowing.add("Product Categories");
    //nowShowing.add("Red 2");
    //nowShowing.add("The Wolverine");

    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("Client");
    comingSoon.add("Client Products");
    comingSoon.add("Client Orders");
    comingSoon.add("Client Orders Delivery");
    //comingSoon.add("Europa Report");

    listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
    listDataChild.put(listDataHeader.get(1), nowShowing);
    listDataChild.put(listDataHeader.get(2), comingSoon);
}

ExpandableTests.java

public class ExpandableTests extends BaseExpandableListAdapter{


      private Context _context;

private List<Integer> _group;

private HashMap<Integer, List<Integer>> _Child;



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

public ExpandableTests(Context context, List<String> listDataHeader,List<Integer> group,
        HashMap<String, List<String>> listDataChild, HashMap<Integer, List<Integer>> Child) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listDataChild;
    this._group=group;
    this._Child=Child;
}


@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
    // this._Child.get(this._group.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);
//  final int childimg = (Integer) 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);

    ImageView imgListChild = (ImageView) convertView.findViewById(R.id.childsid);

    final int childimg = _Child.get(groupPosition).get(childPosition);
    imgListChild.setImageResource(childimg);
    //imgListChild.setImageResource(childimg);

    /*ImageView imageView = (ImageView) convertView.findViewById(R.id.childsid);
    final  int imageId = this.childImages.get(this.groupImages.get(groupPosition)).get(childPosition);
    imageView.setImageResource(imageId);*/

    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);
    //int headerimg = (Integer) 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);

    ImageView imgsListHeader = (ImageView) convertView
            .findViewById(R.id.gropids);

    int headerimg = _group.get(groupPosition);
    imgsListHeader.setImageResource(headerimg);
//  lblListHeader.setTypeface(null, Typeface.BOLD);
    //imgsListHeader.setImageResource(R.drawable.ic_launcher);

    /*ImageView imageView = (ImageView) convertView.findViewById(R.id.gropids);
    int imageId = this.groupImages.get(groupPosition);
    imageView.setImageResource(imageId);*/
    return convertView;
}

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

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

2 个答案:

答案 0 :(得分:0)

您正在尝试将字符串转换为整数

在你的

ExpandableTests class

后面的评论

//in getChildView() method
   // final int childimg = (Integer) getChild(groupPosition, childPosition);

//in getGroupView method
//String headerTitle = (String) getGroup(groupPosition);

并使用任何默认图标来显示以下图片,例如

imgsListHeader.setImageResource(R.drawable.ic_launcher);
imgListChild.setImageResource(R.drawable.ic_launcher);

通过这种方式,您将摆脱崩溃,希望您能够进一步了解图像。

答案 1 :(得分:0)

将以下行替换为getChildView

中的前一行
final int childimg = _Child.get(groupPosition).get(childPosition);
imgListChild.setImageResource(childimg);

将以下行替换为getGroupView中的前一行

int headerimg = _group.get(groupPosition);
imgsListHeader.setImageResource(headerimg);

替换以下行MainActivity

   listAdapter = new ExpandableTests(this, listDataHeader, groupImages, listDataChild, childImages);

中再添加一张图片
  groupImages arraylist in prepareListData method

替换以下行

 childImages.put(groupImages.get(0), Maincat);
 childImages.put(groupImages.get(1), subcat);

 childImages.put(0, Maincat);
 childImages.put(1, subcat);

对于指标图标

在ExpandableTests

中的getGroupView()中添加以下行
ImageView imgIndicator = (ImageView) convertView.findViewById(R.id.indicator);
        if (isExpanded) {
            imgIndicator.setImageResource(R.drawable.minus);
        } else {
            imgIndicator.setImageResource(R.drawable.plus);
        }

在list_group.xml中添加以下imageview

<ImageView
        android:id="@+id/indicator"
        android:layout_width="24sp"
        android:layout_height="24sp"
        android:layout_alignParentRight="true"
       />