0

在我的 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?

谢谢

4

2 回答 2

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;
    }

}
于 2013-07-26T11:28:27.067 回答
0

有一些可能的替代方法可以做到这一点。您可以创建一个静态类,将数据保存在静态字段中。然后你可以通过这个静态类在你的适配器中访问这个对象。您还可以在适配器类中为此类对象定义一个参数。因此,如果您创建适配器的实例,您可以使用特定对象实例化它。

现在您可以访问适配器中的对象并在列表视图中使用它们的信息

伪代码:

public MyAdapter extends BaseAdapter(){
   public MyAdapter(Object myObject){
        this.object = myObject;
   }
   //handleMethods
}

希望它有帮助

干杯

于 2013-07-26T10:53:38.017 回答