在我的Android应用程序中,我已经从XML文件解析数据并将它们放在某个对象中,现在我要读取这些数据并将它们呈现在可扩展的列表视图中。我知道如何在可扩展列表视图中显示数据:我需要一个适配器,但要使用适配器,我首先需要存储在我的对象中的数据,以便如何从对象中读取数据?
编辑:在这里你可以看到我的适配器应该如何。
Myadapter.java
package it.lucgian84.adapter;
import it.lucgian84.models.Episode;
import it.lucgian84.models.Program;
import it.lucgian84.models.Programs;
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
public class Myadapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Program> programList;
public Myadapter(Context context, ArrayList<Program> programList) {
this.context = context;
this.programList = programList;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
Episode[] episodeList = programList.get(groupPosition).getEpisodes();
return episodeList.length;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View view, ViewGroup parent) {
return null;
}
@Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
}
所以在Activity中我应该填充ArrayList programList以在我的适配器中使用。要填充programList,我需要从我的对象中读取信息,如何填充programList?
谢谢
答案 0 :(得分:0)
这样做有一些可能的替代方案。 您可以创建一个静态类,将数据保存在staic字段中。然后,您可以通过此静态类访问适配器中的此对象。 您还可以在适配器类中为此类对象定义参数。因此,如果您创建适配器的实例,则可以使用特定对象实例化它。
现在,您可以访问适配器中的对象并在列表视图中使用它们的信息
伪代码:
public MyAdapter extends BaseAdapter(){
public MyAdapter(Object myObject){
this.object = myObject;
}
//handleMethods
}
希望有所帮助
干杯
答案 1 :(得分:0)
试试这个:
class Myadapter extends BaseExpandableListAdapter {
private Context contProgram
private ArrayList<Program> programList;
public Myadapter(Context context, ArrayList<Program> programList) {
this.context = context;
this.programList = programList;
}
@Override
public Object getChild(int arg0, int arg1) {
return null;
}
@Override
public long getChildId(int arg0, int arg1) {
return 0;
}
@Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3, ViewGroup arg4) {
Episode program = programList.get(arg0).getEpisodes().get(arg1);
//your child view inflate from xml or add programatically
return null;
}
@Override
public int getChildrenCount(int arg0) {
return programList.get(arg0).getEpisodes().lenth;
}
@Override
public Object getGroup(int arg0) {
return programList.size();
}
@Override
public int getGroupCount() {
return 0;
}
@Override
public long getGroupId(int arg0) {
return 0;
}
@Override
public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
Program program = programList.get(arg0);
//your group view inflate from xml or add programatically
return null;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
}