简体   繁体   中英

Get child name from ExpandableListView

I'm learning android and I'm testing right now with ExpandableListViews. I'm doing something like a dictionary with a SearchView. When I click on a child I want to start a new activity with the child name on the ActionBar and the meaning of the word(from strings.xml) in a TextView. The problem is that instead of the child name, the showed text is something like: com.example.jairo_2.myapplication.country@58f8f2e with each child. Could you help me?. I have simplified the code with a Toast that shows the child name. Thanks.

Error image

I have followed Android ExpandableListView Search Filter Example .

MainActivity.java:

package com.example.jairo_2.myapplication;

import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements
    SearchView.OnQueryTextListener, SearchView.OnCloseListener{

private SearchView search;
private MyListAdapter listAdapter;
private ExpandableListView myList;
public ArrayList<Continent> continentList = new ArrayList<Continent>();

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

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    search = (SearchView) findViewById(R.id.search);
    search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    search.setIconifiedByDefault(false);
    search.setOnQueryTextListener(this);
    search.setOnCloseListener(this);


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


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

//method to expand all groups
private void expandAll() {
    int count = listAdapter.getGroupCount();
    for (int i = 0; i < count; i++){
        myList.expandGroup(i);
    }
}

//method to expand all groups
private void displayList() {

    //display the list
    loadSomeData();


    //get reference to the ExpandableListView
    myList = (ExpandableListView) findViewById(R.id.expandableList);
    //create the adapter by passing your ArrayList data
    listAdapter = new MyListAdapter(MainActivity.this, continentList);
    //attach the adapter to the list
    myList.setAdapter(listAdapter);

}

private void loadSomeData() {

    ArrayList<Country> countryList = new ArrayList<Country>();
    Country country = new Country("word1");
    countryList.add(country);
    country = new Country("word2");
    countryList.add(country);
    country = new Country("word3");
    countryList.add(country);

    Continent continent = new Continent("A",countryList);
    continentList.add(continent);

    countryList = new ArrayList<Country>();
    country = new Country("China");
    countryList.add(country);
    country = new Country("Japan");
    countryList.add(country);
    country = new Country("Thailand");
    countryList.add(country);

    continent = new Continent("Asia",countryList);
    continentList.add(continent);

}

@Override
public boolean onClose() {
    listAdapter.filterData("");
    expandAll();


    return false;
}

@Override
public boolean onQueryTextChange(String query) {


    listAdapter.filterData(query);
    expandAll();

    if (query.compareTo("") == 0){
        int count =  listAdapter.getGroupCount();
    for (int i = 0; i <count ; i++)
        myList.collapseGroup(i);}

    return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
    listAdapter.filterData(query);
    expandAll();

    if (query.compareTo("") == 0){
        int count =  listAdapter.getGroupCount();
        for (int i = 0; i <count ; i++)
            myList.collapseGroup(i);}
    return false;
}
void setListener() {

    // This listener will show toast on group click
    myList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView listview, View view,
                                    int group_pos, long id) {

            Toast.makeText(MainActivity.this, "You clicked : " + listAdapter.getGroup(group_pos), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // This listener will expand one group at one time
    // You can remove this listener for expanding all groups


    //myList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

                // Default position
                //int previousGroup = -1;

                //@Override
                //public void onGroupExpand(int groupPosition) {
                 //   if (groupPosition != previousGroup)

                        // Collapse the expanded group
                     //   myList.collapseGroup(previousGroup);
                    //previousGroup = groupPosition;
              //  }

           // });

    // This listener will show toast on child click
    myList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {


        @Override
        public boolean onChildClick(ExpandableListView listview, View view,
                                    int groupPos, int childPos, long id) {

            Toast.makeText(MainActivity.this, "You clicked : " + listAdapter.getChild(groupPos,childPos), Toast.LENGTH_SHORT).show();

            return false;
        }
    });
}

}

MyListAdapter.java:

package com.example.jairo_2.myapplication;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;


public class MyListAdapter extends BaseExpandableListAdapter {

private Context context;
public ArrayList<Continent> continentList;
private ArrayList<Continent> originalList;

public MyListAdapter(Context context, ArrayList<Continent> continentList) {
    this.context = context;
    this.continentList = new ArrayList<Continent>();
    this.continentList.addAll(continentList);
    this.originalList = new ArrayList<Continent>();
    this.originalList.addAll(continentList);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList();
    return countryList.get(childPosition);
}

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

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                         View view, ViewGroup parent) {

    Country country = (Country) getChild(groupPosition, childPosition);
    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.child_row, null);
    }


    TextView name = (TextView) view.findViewById(R.id.name);


    name.setText(country.getName().trim());

    return view;
}

@Override
public int getChildrenCount(int groupPosition) {

    ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList();
    return countryList.size();

}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isLastChild, View view,
                         ViewGroup parent) {

    Continent continent = (Continent) getGroup(groupPosition);
    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.group_row, null);
    }

    TextView heading = (TextView) view.findViewById(R.id.heading);
    heading.setText(continent.getName().trim());

    return view;
}

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

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

public void filterData(String query){

    query = query.toLowerCase();
    Log.v("MyListAdapter", String.valueOf(continentList.size()));
    continentList.clear();

    if(query.isEmpty()){
       continentList.addAll(originalList);


    }
    else {

        for(Continent continent: originalList){

            ArrayList<Country> countryList = continent.getCountryList();
            ArrayList<Country> newList = new ArrayList<Country>();
            for(Country country: countryList){
                if(country.getName().toLowerCase().contains(query)){
                    newList.add(country);
                }
            }
            if(newList.size() > 0){
                Continent nContinent = new Continent(continent.getName(),newList);
                continentList.add(nContinent);
            }
        }
    }

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

}

}

Country.java:

package com.example.jairo_2.myapplication;


public class Country {


private String name = "";


public Country(String name) {
    super();

    this.name = name;

}


public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

Continent.java

package com.example.jairo_2.myapplication;

import java.util.ArrayList;


public class Continent {

private String name;
private ArrayList<Country> countryList = new ArrayList<Country>();

public Continent(String name, ArrayList<Country> countryList) {
    super();
    this.name = name;
    this.countryList = countryList;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public ArrayList<Country> getCountryList() {
    return countryList;
}
public void setCountryList(ArrayList<Country> countryList) {
    this.countryList = countryList;
};


}

在Toast消息部分中像这样的continentList.get(groupPosition).getCountryList().get(childposition)使用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM