我正在尝试创建一个Android应用程序,该应用程序获取有关相册的信息并将其显示在可扩展列表视图中。艺术家的名称将显示为父项目,当选择艺术家时,将显示该艺术家的相册作为孩子。通过初始化MyExpandableListAdapter类中的字符串,我已经能够使列表工作到目前为止。但是,我希望能够按下按钮并将新视图添加到包含信息的列表中。
这是我的代码:
package droid.musiclibrary;
/**
* Some code taken from:
* http://www.dreamincode.net/forums/topic/270612-how-to-get-started-with-expandablelistview/
*/
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ExpandableListActivity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class AddContent extends Activity {
private Spinner spinArtist, spinGenre;
private EditText addArtist, addGenre, addTitle, addYear;
private ArrayAdapter<String> adapterArtist, adapterGenre;
private ArrayList<String> artists = new ArrayList<String>();
private ArrayList<String> genres = new ArrayList<String>();
private ArrayList<String> albums = new ArrayList<String>();
private ArrayList<String> albumList = new ArrayList<String>();
List<List<String>> albumArray = new ArrayList<List<String>>();
private ExpandableListView expList;
private MyExpandableListAdapter expAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.add_content);
expList = (ExpandableListView) findViewById(R.id.artistList);
expAdapter = new MyExpandableListAdapter(this);
expList.setAdapter(expAdapter);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This is called when the Home (Up) button is pressed
// in the Action Bar.
Intent parentActivityIntent = new Intent(this, MainActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public AddContent() {
}
public void addArtist(View view) {
spinArtist = (Spinner) findViewById(R.id.spinArtist);
addArtist = (EditText) findViewById(R.id.txtAddArtist);
if(addArtist.getText().toString().trim().equals(""))
{
Context context = getApplicationContext();
CharSequence text = "You need to enter an artist.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
artists.add(addArtist.getText().toString());
adapterArtist = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, artists);
adapterArtist.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinArtist.setAdapter(adapterArtist);
addArtist.setText("");
}
public void addGenre(View view) {
spinGenre = (Spinner) findViewById(R.id.spinGenre);
addGenre = (EditText) findViewById(R.id.txtAddGenre);
if(addGenre.getText().toString().trim().equals(""))
{
Context context = getApplicationContext();
CharSequence text = "You need to enter a genre.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
genres.add(addGenre.getText().toString());
adapterGenre = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, genres);
adapterGenre.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinGenre.setAdapter(adapterGenre);
addGenre.setText("");
}
public void addAlbum(View view) {
addTitle = (EditText) findViewById(R.id.txtAddTitle);
addYear = (EditText) findViewById(R.id.txtAddYear);
if(addTitle.getText().toString().trim().equals(""))
{
Context context = getApplicationContext();
CharSequence text = "You need to enter a title.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else if(spinArtist.getSelectedItem().toString().trim().equals(""))
{
Context context = getApplicationContext();
CharSequence text = "You need to select an artist.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else if(spinGenre.getSelectedItem().toString().trim().equals(""))
{
Context context = getApplicationContext();
CharSequence text = "You need to select a genre.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else if(addYear.getText().toString().trim().equals(""))
{
Context context = getApplicationContext();
CharSequence text = "You need to enter a year.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
albums.add(addTitle.getText().toString().trim());
}
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
String[] groups = {"Silverstein"};
String[][] children = {{"Discovering the Waterfront"}};
public MyExpandableListAdapter(Context context) {
this.context = context;
}
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView tv = new TextView(this.context);
tv.setLayoutParams(lp);
// Center the text vertically
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
tv.setPadding(45, 0, 0, 0);
return tv;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
/*public void setArtist(ArrayList<String> artist) {
this.artists = artist;
}
public ArrayList<String> getArtist() {
return artist;
}
public void setAlbum(ArrayList<String> album) {
this.albums = album;
}
public ArrayList<String> getAlbum() {
return albums;
}
/**
* This will actually be used to sort the added content.
* For example, when sorting by artist, the parent will be the artist's name
* and the child entries will be the albums by that artist.
* This will probably have to moved to a separate class (Sort.java???)
*/
/*public void addItem(ExpandListChild item, ExpandListParent group) {
if(!artistList.contains(group)) {
artistList.add((group));
}
int index = artistList.indexOf(group);
ArrayList<ExpandListChild> ch = artistList.get(index).getItems();
ch.add(item);
artistList.get(index).setItems(ch);
}
public Object getChild(int parentPosition, int childPosition) {
ArrayList<ExpandListChild> chList = artistList.get(parentPosition).getItems();
return chList.get(childPosition);
}
public long getChildId(int parentPosition, int childPosition) {
return childPosition;
}*/
}
有没有办法将艺术家和专辑ArrayList添加到MyExpandableListAdpter类中的String []?我已经尝试了很多不同的方法,每次我得到一个NullPointerException。
任何帮助将不胜感激,我花了几个小时寻找答案。
答案 0 :(得分:0)
投掷SimpleExpandableListAdapter并维护对您的组(父级)和子级数据对象的引用。
然后它就像:
一样简单mChildData.get(groupIndex).get(childIndex).put(...);
mListAdapter.notifyDataSetChanged();