单击菜单项后显示可扩展列表

时间:2011-12-03 18:22:55

标签: android listview menu click

我想在点击菜单项后显示Expandablelist这样做我已按照本教程link

但是这里的Expandablelist在main.xml而不是我在另一个xml文件中。 我已经使用了这一行,因为我没有使用ExpandableListActivity扩展myapp,但是如果我将Activity替换为ExpandableListActivity我的应用程序无法工作:

expList = getExpandableListView();//red inderligned line

我的代码

public class Myapp extends Activity implements OnClickListener {
static final String[][] exp_channels = {
    {"Nazionale", "Abruzzo", "Altoadige","Basilicata", "Calabria", "Campania", "Emilia", "Friuli", "Lazio", "Liguria", "Lombardia", 
    "Marche", "Molise", "Piemonte", "Puglia", "Sardegna", "Sicilia", "Toscana", "Trentino", "Umbria", "Aosta", "Veneto" },
};

DisplayMetrics metrics;
 int width;
 ExpandableListView expList;

public boolean onCreateOptionsMenu(Menu menu)
 {

    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
 }
public Dialog onCreateDialog(int id) {

     switch (id) {
     case 1: //Country Menu item

            metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            width = metrics.widthPixels;
            expList = getExpandableListView();
            //this code for adjusting the group indicator into right side of the view
            //expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));
            expList.setAdapter(new ExpAdapter(this));

      expList.setOnGroupExpandListener(new OnGroupExpandListener()
      {
       //@Override
       public void onGroupExpand(int groupPosition) 
       {
        //Log.e("onGroupExpand", "OK");
       }
      });

      expList.setOnGroupCollapseListener(new OnGroupCollapseListener()
      {
       //@Override
       public void onGroupCollapse(int groupPosition) 
       {
        //Log.e("onGroupCollapse", "OK");
       }
      });

      expList.setOnChildClickListener(new OnChildClickListener()
      {
       //@Override
       public boolean onChildClick(ExpandableListView parent, View v,
         int groupPosition, int childPosition, long id) {
        //Log.e("OnChildClickListener", "OK");
        return false;
       }
      });
         break;
}

   AlertDialog MyAlert  = alert.create();
    return MyAlert;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     case R.id.country:
         onCreateDialog(1).show();
         break;
     }
     return false;
 }

public class ExpAdapter extends BaseExpandableListAdapter {

      private Context myContext;
      public ExpAdapter(Context context) {
       myContext = context;
      }
      //@Override
      public Object getChild(int groupPosition, int childPosition) {
       return null;
      }

      //@Override
      public long getChildId(int groupPosition, int childPosition) {
       return 0;
      }

      //@Override
      public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_row, null);
       }

       TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
       tvPlayerName.setText(exp_channels[groupPosition][childPosition]);

       return convertView;
      }

      //@Override
      public int getChildrenCount(int groupPosition) {
       return exp_channels[groupPosition].length;
      }

      //@Override
      public Object getGroup(int groupPosition) {
       return null;
      }

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

      //@Override
      public long getGroupId(int groupPosition) {
       return 0;
      }

      //@Override
      public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

       if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_row, null);
       }

       TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName);
       tvGroupName.setText(lang_country[groupPosition]);

       return convertView;
      }

      //@Override
      public boolean hasStableIds() {
       return false;
      }

      //@Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
       return true;
      }
     }

myexplist:

<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ExpandableListView>

group_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<linearlayout android:id="@+id/groupname" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="40dip">

     <TextView android:id="@+id/tvGroupName" android:layout_width="wrap_content" android:layout_height="40dip" 
         android:textSize="16sp" android:textStyle="bold" android:paddingLeft="30dip" android:gravity="center_vertical">
         </TextView>
</linearlayout>

child_row.xml

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" 
android:layout_width="fill_parent" android:layout_height="40dip" android:gravity="center_vertical">
<textview android:id="@+id/tvPlayerName" android:paddingLeft="50dip" android:textSize="14sp" android:layout_width="wrap_content"
     android:layout_height="30dip" android:gravity="center_vertical">
    </textview>  
</linearlayout>

0 个答案:

没有答案