从 ExpandableListView 获取 child 名称
Get child name from ExpandableListView
我正在学习 android 并且我现在正在使用 ExpandableListViews 进行测试。我正在做一些类似于带有 SearchView 的字典。当我单击 child 时,我想在 ActionBar 上使用 child 名称并在 TextView 中使用单词的含义(来自 strings.xml)来启动一个新的 activity。问题是显示的文本不是 child 名称,而是类似于:com.example.jairo_2.myapplication.country@58f8f2e 每个 child。你可以帮帮我吗?。我使用显示 child 名称的 Toast 简化了代码。谢谢。
我已关注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;
};
}
像这样使用continentList.get(groupPosition).getCountryList().get(childposition)
在您的 Toast 消息部分
我正在学习 android 并且我现在正在使用 ExpandableListViews 进行测试。我正在做一些类似于带有 SearchView 的字典。当我单击 child 时,我想在 ActionBar 上使用 child 名称并在 TextView 中使用单词的含义(来自 strings.xml)来启动一个新的 activity。问题是显示的文本不是 child 名称,而是类似于:com.example.jairo_2.myapplication.country@58f8f2e 每个 child。你可以帮帮我吗?。我使用显示 child 名称的 Toast 简化了代码。谢谢。
我已关注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;
};
}
像这样使用continentList.get(groupPosition).getCountryList().get(childposition)
在您的 Toast 消息部分