这可能是一个非常简单的问题,但我正在寻找你的帮助。 我使用下载的开源文件。我有一个包含填充儿童的群组列表。我能够扩展列表并查看孩子的内容。如何在点击特定儿童时打开带有文本信息关联器的新活动窗口?
感谢您的支持。
MainActivity.java:
package com.example.expandablelistviewsearch;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.ExpandableListView.OnChildClickListener;
import java.util.ArrayList;
public class MainActivity extends Activity implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener {
private SearchView search;
private MyListAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Continent> continentList = new ArrayList<Continent>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myList.setOnChildClickListener(myListItemClicked); //added line
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(true);
search.setOnQueryTextListener(this);
//search.setOnCloseListener(this);
// display the list
displayList();
// expand all Groups
//expandAll();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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);
}
}
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//get the group header
HeaderInfo headerInfo = deptList.get(groupPosition);
//get the child info
DetailInfo detailInfo = headerInfo.getProductList().get(childPosition);
//start new activity with specific child information
return false;
}
};
// 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("Test1", "Test2", "Test3");
countryList.add(country);
Continent continent = new Continent("Main Test", countryList);
continentList.add(continent);
private void loadSomeData() {
ArrayList<Country> countryList = new ArrayList<Country>();
Country country = new Country("Test1", "Test2", "Test3");
countryList.add(country);
Continent continent = new Continent("Main Tes1t", countryList);
continentList.add(continent);
private void loadSomeData() {
ArrayList<Country> countryList = new ArrayList<Country>();
Country country = new Country("Test1", "Test2", "Test3");
countryList.add(country);
Continent continent = new Continent("Main Test2", countryList);
continentList.add(continent);
}
@Override
public boolean onClose() {
// TODO Auto-generated method stub
listAdapter.filterData("");
//expandAll();
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
listAdapter.filterData(newText);
expandAll();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
listAdapter.filterData(query);
expandAll();
return false;
}
}
MyListAdapter.java
package com.example.expandablelistviewsearch;
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;
private 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) {
// TODO Auto-generated method stub
ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList();
return countryList.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 convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Country country = (Country) getChild(groupPosition, childPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_row, null);
}
TextView code = (TextView) convertView.findViewById(R.id.code);
TextView name = (TextView) convertView.findViewById(R.id.name);
TextView fdd = (TextView) convertView.findViewById(R.id.fdd);
code.setText(country.getCode().trim());
name.setText(country.getName().trim());
fdd.setText(country.getFdd().trim());
//population.setText(NumberFormat.getNumberInstance(Locale.US).format(country.getPopulation()));
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList();
return countryList.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return continentList.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return continentList.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Continent continent = (Continent) getGroup(groupPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.group_row, null);
}
TextView heading = (TextView) convertView.findViewById(R.id.heading);
heading.setText(continent.getName().trim());
return convertView;
}
@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 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.getCode().toLowerCase().contains(query) || 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();
}
}
答案 0 :(得分:0)
您几乎就在那里,根据您的代码,您可以使用意图将与您点击的孩子相关的任何内容发送到新活动,然后在新活动中获取信息
private OnChildClickListener myListItemClicked = new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//get the group header
HeaderInfo headerInfo = deptList.get(groupPosition);
//get the child info
DetailInfo detailInfo = headerInfo.getProductList().get(childPosition);
//start new activity with specific child information
//--Add below codes to your code
Intent myIntent = new Intent(MainActivity.this, TargetActivity.class);
myIntent.putExtra("information",detailInfo.getInfor());
return false;
}
};
在TargetActivity中,
Intent tIntent= getIntent();
Bundle b = tIntent.getExtras();
String information = (String)b.get("information");
字符串信息可能是您想要显示的内容