可扩展列表视图仅显示一个子项

时间:2014-12-02 11:42:49

标签: android listview expandablelistview

我有一个可扩展的列表视图也通过arraylist传递父项和子项..我只能看到第一个父项...通过点击其他项没有任何反应

活动和可扩展列表视图适配器代码如下所示

	expandableList = (ExpandableListView) findViewById(R.id.exlist);
		
		expandableList.setDividerHeight(2);
		expandableList.setGroupIndicator(null);
		expandableList.setClickable(true);

		setGroupParents();
		setChildData();

		MyExpandableAdapter exadapter = new MyExpandableAdapter(data);
		
		exadapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
		expandableList.setAdapter(exadapter);
//		expandableList.setOnChildClickListener(this);
		expandableList.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				return false;
			}
		});

package com.javacodegeeks.android.loginapp;

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 String parent;
	private LayoutInflater inflater;
	private ArrayList<String> parentItems;
	ArrayList<ObjectModelClass> data;
	ArrayList<ArrayList<String>> nodes = new ArrayList<ArrayList<String>>();

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

	public MyExpandableAdapter(ArrayList<ObjectModelClass> data) {
		
		this.data=data;
		
		
	}

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

	
	
	@Override
	public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		
		String child = (String) getChild(groupPosition, childPosition);
		ChildViewHolder childHolder;
		
		if (convertView == null) {
			childHolder	=	new ChildViewHolder();
			convertView = inflater.inflate(R.layout.group, null);
			childHolder.textView = (TextView) convertView.findViewById(R.id.title);
			childHolder.textPrice = (TextView) convertView.findViewById(R.id.price);
			convertView.setTag(childHolder);
		}
		else
		{
			childHolder	=	(ChildViewHolder) convertView.getTag();
		}
		
		
		childHolder.textView.setText(child);
		
		
		
		return convertView;
	}
	
	private static class ChildViewHolder
	{
		TextView textView;
		TextView textPrice;
	}
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.row_ex, null);
		}
		
		((CheckedTextView) convertView).setText(data.get(groupPosition).getTitle());
		((CheckedTextView) convertView).setChecked(isExpanded);
		
		return convertView;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return data.get(groupPosition).getChildren().get(childPosition);
	}

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

	
	@Override
	public int getChildrenCount(int groupPosition) {
		System.out.println(groupPosition);
		return data.get(groupPosition).getChildren().size();
	}

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

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



	
	
	

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

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

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

}

2 个答案:

答案 0 :(得分:0)

改变这个:

@Override
public Object getChild(int groupPosition, int childPosition) {
    return data.get(groupPosition).getChildren().get(childPosition);
}

答案 1 :(得分:0)

将子计数更改为data.get(groupPosition).getChildren()。size();