开发者

Android Custom View and BaseExpandableListAdapter

I want to add a custom view to a group view with a BaseExpandableListAdapter

This is The code:

The custom view and layout.xml

public class InfoView extends View{
    private EditText nameEditText;
    private EditText descriptionEditText;
    private Spinner goalTypeSpinner;
    private Spinner categorySpinner;



    Button addGoalCategoryButton;
    public InfoView(Context context) {
        super(context);
        inflate(context, R.layout.info_panel, parent);
        nameEditText = (EditText) findViewById(R.id.nameEditText);

        descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);

        goalTypeSpinner = (Spinner) 
                findViewById(R.id.goalTypeSpinner);

        categorySpinner = (Spinner) 
                findViewById(R.id.categorySpinner);
    }
}

infopanel.xml

<LinearLayout 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" >
    <Spinner android:id=开发者_运维问答"@+id/goalTypeSpinner" android:prompt="@string/goalType"
            android:entries="@array/goalTypeList" style="@style/commonText.commonSpinner" >
    </Spinner>
    <TableRow style="@style/commonText.commonTableRow">

            <Spinner android:id="@+id/categorySpinner" style="@style/commonText.commonSpinner.right"
                android:prompt="@string/goalType">
            </Spinner>
            <Button android:id="@+id/newCategoryButton" style="@style/commonText.commonButton"
                android:text="add category" />
    </TableRow> 
    <EditText android:id="@+id/nameEditText" android:hint="@string/nameEditTextHint"
        style="@style/commonText.commonEditText" >
    </EditText>
    <EditText android:id="@+id/descriptionEditText" android:hint="@string/descriptionEditTextHint"
        style="@style/commonText.commonTextArea">
    </EditText>

</LinearLayout>

In the BaseExpandableListAdapter :

..............
InfoView infoView;
public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        if (group.equals(INFO) && child.equals(INFO)) {
            Log.i(TAG, "group INFO(" + group + ")");

            if (infoView == null) {

                // i don't want to recreate everytime 
                // the view so i check if is null         

                infoView = new InfoView(context);
            }

            return infoView;
        }
........

The code works (ie doesn't throw any exception) but the view isn't displayed.

Do you know Why ?

thanks and regards


The proper way to implement getChildView is to use the provided convertView. Don't keep reference of any Views created in getChildView.

public View getChildView(int groupPosition, int childPosition,
                        boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = new InfoView(context);
    }
    return convertView;
}


There's a lot missing from the code you've posted: Where is infopanel.xml used? How do you populate InfoView's child views with data? Etc.

But going from what you have posted, it looks like you're trying to create what's called a Compound Control. Such controls (apparently) need to derive from a Layout, such as LinearLayout, that manages the child views appropriately. Deriving from View as you've done would work only if you had added a lot more code to your View class that you haven't shown here -- for example, implementations of onMeasure and onDraw.

I suggest you read the docs on Compound Controls, if you haven't already.

gngr44's answer is also correct -- you shouldn't be trying to reuse an InfoView in that way. Trust the Android code to recycle the views for you, and use the convertView if it's not null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜