0

我正在创建一个带有 ListView 的菜单,其中有一个具有不同类型的路线的划分:历史、自然主义等。

package it.sii.android.helloandroid;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;

public class HelloAndroid extends ExpandableListActivity {

ExpandableListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set up our adapter
    mAdapter = new MyExpandableListAdapter();
    setListAdapter(mAdapter);
    registerForContextMenu(getExpandableListView());
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menu.setHeaderTitle("Routes");
    menu.add(0, 0, 0, R.string.hello_world);
}


@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)  item.getMenuInfo();

    String title = ((TextView) info.targetView).getText().toString();

    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
        int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
        Toast.makeText(this, title + ": Figlio " + childPos + " cliccato nel gruppo " + groupPos,
                Toast.LENGTH_SHORT).show();
        return true;
    } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
        int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
        Toast.makeText(this, title + ": Gruppo " + groupPos + " cliccato", Toast.LENGTH_SHORT).show();
        return true;
    }

    return false;
}


public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    private String[] groups = { "Historical Routes", "Naturalistic Routes", "Trendy&Shopping Routes", "Cultural Routes" };
    private String[][] children = {
            { "Porta Nuova - Piazza Castello", "The Roman route"},
            { "Superga hill", "Maddalena route"},
            { "From Porta Palazzo to Balon", "Via Roma" },
            { "Savoian age route", "Rinascimental route", "Scientific and industrial route" }
    };

    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() {
        // Parametri per il Layout ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(HelloAndroid.this);
        textView.setLayoutParams(lp);
        // Centra il testo verticalmente
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Setta la posiione di partenza del testo
        textView.setPadding(36, 0, 0, 0);



        return textView;
    }

    public boolean onOptionsItemSelected(MenuItem item){

        return false;
    }
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).toString());

        textView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                Intent i = new Intent(HelloAndroid.this, NuovaActivity.class);
                startActivity(i);



            }







        });

        return textView;
    }

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

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

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

}
}

现在的问题是,当我想点击 Porta Nuova-Piazza Castello 语音时,我必须有一个活动(它将通过菜单包含技术信息和地理定位)。现在的问题是,如果我单击另一个声音,例如 Roman Route,我将拥有与 Porta NUova Piazza Castello 相同的活动。如何为不同的声音关联一个分别的活动???

我以为问题出在这里

public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).toString());

        textView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                Intent i = new Intent(HelloAndroid.this, NuovaActivity.class);
                startActivity(i);



            }

请帮我!!!谢谢

4

1 回答 1

0

当您开始活动时,您不会告诉活动有关用户选择的任何内容。如何NuovaActivity知道用户点击了什么以便显示正确的信息?

您可能想向Intent用于 start的数据添加一些数据NuovaActivity,如下所示:

public void onClick(View v) {
    Intent i = new Intent(HelloAndroid.this, NuovaActivity.class);
    // add an extra so that the new activity knows which item the user selected
    i.putExtra("clickedItem", ((TextView)v).getText().toString()));
    startActivity(i);
}
于 2013-05-17T13:47:36.043 回答