处理 chlidclick expandablelistview

Handle chlidclick expandablelistview

我用搜索视图制作了一个简单的可扩展列表视图 我想祝酒词 当我点击 child 项目时,它说项目 ID 已选中,但我不能

这是我的 class MyLislist 大洲是群体 和国家 children


private Context context2;
private ArrayList<Continent2> continentList2;
private ArrayList<Continent2> originalList2;

public MyListAdapter2(Context context2, ArrayList<Continent2> continentList2) {
    this.context2 = context2;
    this.continentList2 = new ArrayList<Continent2>();
    this.continentList2.addAll(continentList2);
    this.originalList2 = new ArrayList<Continent2>();
    this.originalList2.addAll(continentList2);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    ArrayList<Country2> countryList2 = continentList2.get(groupPosition).getCountryList2();
    return countryList2.get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
}

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

    Country2 country2 = (Country2) getChild(groupPosition, childPosition);
    if(convertView2 == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) context2.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView2 = layoutInflater.inflate(R.layout.child_row2, null);
    }

    TextView name2 = (TextView) convertView2.findViewById(R.id.name2);
    name2.setText(country2.getName2().trim());

    name2.setTextSize(25);
    name2.setPadding(50,0,0,0);

    return convertView2;
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    ArrayList<Country2> countryList2 = continentList2.get(groupPosition).getCountryList2();
    return countryList2.size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return continentList2.get(groupPosition);
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return continentList2.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView2, ViewGroup parent) {
    // TODO Auto-generated method stub
    Continent2 continent2 = (Continent2) getGroup(groupPosition);
    if(convertView2 == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater) context2.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView2 = layoutInflater.inflate(R.layout.group_row2, null);
    }

    TextView heading2 = (TextView) convertView2.findViewById(R.id.heading2);
    heading2.setText(continent2.getName2().trim());

    heading2.setTextSize(30);
    heading2.setPadding(20, 0, 0, 0);




    return convertView2;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return false;
}

public void filterData(String query)
{
    query = query.toLowerCase();
    Log.v("MyListAdapter", String.valueOf(continentList2.size()));
    continentList2.clear();

    if(query.isEmpty())
    {
        continentList2.addAll(originalList2);
    } else {
        for(Continent2 continent2: originalList2)
        {
            ArrayList<Country2> countryList2 = continent2.getCountryList2();
            ArrayList<Country2> newList = new ArrayList<Country2>();
            for(Country2 country2: countryList2)
            {
                if( country2.getName2().toLowerCase().contains(query))
                {
                    newList.add(country2);
                }
            }
            if(newList.size() > 0)
            {
                Continent2 nContinent2 = new Continent2(continent2.getName2(), newList);
                continentList2.add(nContinent2);
            }
        }
    }

    Log.v("MyListAdapter", String.valueOf(continentList2.size()));
    notifyDataSetChanged();
}

}


这是我的 oncreate


enter code here
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maladbd);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    search = (SearchView) findViewById(R.id.search2);
    search.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setOnCloseListener(this);



    // display the list
    displayList();
    // expand all Groups
    expandAll();


}

只需将 setOnChildClickListener 添加到您的列表中,如下所示:

yourList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(getApplicationContext(), "Your message", Toast.LENGTH_SHORT)
                        .show();
                return true;
            }
        });