开发者

Like to know how to get selected item in Expandable List ANDROID

开发者 https://www.devze.com 2023-03-24 04:46 出处:网络
I have an expandable list that contains groups : continents and child : countries. When one country is clicked i want the country to be displayed in a textview on another class.

I have an expandable list that contains groups : continents and child : countries. When one country is clicked i want the country to be displayed in a textview on another class.

package com.zeus.eca;

import android.app.ExpandableListActivity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class ListCountries extends ExpandableListActivity implements                 ExpandableListView.OnChildClickListener{

ExpandableListAdapter mAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mAdapter=new MyExpandableListAdapter();;
    setListAdapter(mAdapter);
    getExpandableListView().setOnChildClickListener(this); 
};
@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    // TODO Auto-generated method stub


    return false;
        //return true;
}
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    // Sample data set.  children[i] contains the children (String[]) for groups[i].
    private String[] groups ={"Africa","Asia","Europe","North America","South    America","Oceania","Antartica"};
    Resources res = getResources();
    private String[] strAfrica=res.getStringArray(R.array.arrayAfrica);
    private String[] strAsia=res.getStringArray(R.array.arrayAsia);
    private String[] strEurope=res.getStringArray(R.array.arrayEurope);
    private String[] strNAmerica=res.getStringArray(R.array.arrayNAmerica);
    private String[] strSAmerica=res.getStringArray(R.array.arraySAmerica);
    private String[] strOceania=res.getStringArray(R.array.arrayOceania);
    private String[] strAntartica=res.getStringArray(R.array.arrayAntartica);
    private String[][] children = {
            strAfrica,strAsia,strEurope,strNAmerica,strSAmerica,strOceania,strAntartica
    };

    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, 55);

        TextView textView = new TextView(ListCountries.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
    //    textView.setBackgroundResource(R.drawable.colorWhite);
      //  textView.setTex开发者_如何转开发tColor(getResources().getColor(R.color.colorBackground));
        return textView;
    }

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

}

}


This should work

@Override
public boolean onChildClick(ExpandableListView parent, View v,
    int groupPosition, int childPosition, long id) {
    String country = (String)mAdapter.getChild(groupPosition, childPosition);
    // update the text view with the country
    return true;
}
0

精彩评论

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