English 中文(简体)
扩音器
原标题:Android ExpandableListView
  • 时间:2010-12-01 15:04:57
  •  标签:
  • android

最近,我试图利用和淡化可扩展的清单观点,因此,我 around绕了这一看法并 st了。

我几乎完全遵循了这些步骤,但我只建立了一个文件,以控制我自己的适应者的执行情况。 我所说的主要活动的基本方法是:

mEntries = findViewById(R.id.entries);    
ExpandableListAdapter adapter = new MyExpandableListAdapter(this);
mEntries.setAdapter(adapter);

本条准则(摘自URL并修改):

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    private String[] groups = {"Vehicle", "Baj"};
    private String[][] children = {
        {"Mol", "Mor"},
        {"In", "Ruh"}
    };

    private Context cxt;

    public MyExpandableListAdapter(Context cxt) {
        this.cxt = cxt;
    }

    @Override
    public Object getChild(int groupPos, int childPos) {
        return children[groupPos][childPos];
    }

    @Override
    public long getChildId(int groupPos, int childPos) {
        return childPos;
    }

    @Override
    public View getChildView(int groupPos, int childPos,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView tv = getGenericView();
        tv.setText(getChild(groupPos, childPos).toString());
        return tv;
    }

    @Override
    public int getChildrenCount(int groupPos) {
        return children[groupPos].length;
    }

    @Override
    public Object getGroup(int groupPos) {
        return groups[groupPos];
    }

    @Override
    public int getGroupCount() {
        return groups.length;
    }

    @Override
    public long getGroupId(int groupPos) {
        return groupPos;
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, 64);

        TextView tv = new TextView(this.cxt);
tv.setLayoutParams(lp);

        // Center the text vertically
        tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        tv.setPadding(36, 0, 0, 0);
        return tv;
    }

    @Override
    public View getGroupView(int groupPos, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView tv = getGenericView();
        tv.setText(getGroup(groupPos).toString());
        return null;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPos, int childPos) {
        return true;
    }

}

log:

12-01 22:24:54.883: ERROR/AndroidRuntime(14424): java.lang.NullPointerException
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.AbsListView.obtainView(AbsListView.java:1276)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.ListView.makeAndAddView(ListView.java:1668)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.ListView.fillDown(ListView.java:637)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.ListView.fillFromTop(ListView.java:694)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.ListView.layoutChildren(ListView.java:1521)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:900)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1119)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:998)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.LinearLayout.onLayout(LinearLayout.java:918)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.view.View.layout(View.java:6830)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
12-01 22:24:54.883: ERROR/AndroidRuntime(14424):     at android.view.View.layout(View.java:6830)
snip...

任何人都想对此作出说明? =

EDIT: I ve already implemented the methods required in the Base class.

EDIT2: Code added

最佳回答

最后,...... :

    @Override
public View getGroupView(int groupPos, boolean isExpanded, View convertView,
        ViewGroup parent) {
    TextView tv = getGenericView();
    tv.setText(getGroup(groupPos).toString());
    return null;
}

页: 1 在I m睡觉=/

问题回答

当你延长可扩展的ListAdapter时,你仍需要执行如何显示每一行。 否则,我认为,目前的 imp是把你看到的那点归来。 因此,我还要执行:

public int getGroupCount()
public int getChildrenCount(int groupPosition)
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

我也执行了其他职能,但我没有显示这些职能。 希望这一帮助。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签