android - 想知道如何在可扩展列表中获取选定的项目 ANDROID

标签 android listview android-layout onclick expandablelistview

我有一个可扩展列表,其中包含组:大陆和子组:国家。单击一个国家/地区时,我希望该国家/地区显示在另一个类的 TextView 中。

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.setTextColor(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;
    }

}

最佳答案

这应该可行

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

关于android - 想知道如何在可扩展列表中获取选定的项目 ANDROID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6884031/

相关文章:

android - 用于 Unity 的 Windows 到 ios 文件?

Android:更改溢出菜单的宽度

c# - 在 WINFORM 中,listView ColumnWidthChanging 事件未触发或替代以禁用调整列大小

javascript - 列表项的可变高度

android - 如何使 LinearLayout 可点击

java - 使用多个 View 以编程方式/动态创建 LinearLayout

java - Horizo​​ntalScrollView 和当前按钮

Android 应用程序检测其他 Activity

android - 如何强制 Marshmallow 打瞌睡?

c# - WPF 嵌套 ListView ItemsSource