这是一个漂亮的千篇一律的程序,它扩展了一个列表,然后当你点击一个孩子时,它会弹出一条消息,说“孩子点击了”。但我希望可扩展列表包含食谱,以便在单击时显示一个弹出窗口的成分。我尝试将其设置为对象数组列表而不是字符串,并让对象包含成分列表,但是在尝试显示成分时我感到很困惑..提前致谢!
package com.poe.poeguide;
import java.util.ArrayList;
import com.actionbarsherlock.app.SherlockActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.view.MenuItem;
import android.widget.ExpandableListView.OnChildClickListener;
public class Recipes extends SherlockActivity {
private ExpandableListView mExpandableList;
/** An array of strings to populate dropdown list */
String[] actions = new String[] {
"Recipes",
"Main Page",
"Attributes",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), R.layout.sherlock_spinner_item, actions);
/** Enabling dropdown list navigation for the action bar */
getSupportActionBar().setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
break;
case 1:
Intent a = new Intent(Recipes.this, MainActivity.class);
startActivity(a);
break;
}
return false;
}
};
getSupportActionBar().setListNavigationCallbacks(adapter, navigationListener);
adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//======================================================================================
//here we set the parents and the children
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Pies");
arrayChildren.add("Apple Pie ");
arrayChildren.add("Blueberry Pie ");
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
//======================================================================================
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(Recipes.this,arrayParents));
mExpandableList.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
{
Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.activity_recipes, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}