将ListView的不同语音与不同的活动相关联

时间:2013-05-17 10:50:57

标签: android android-intent menu android-listview android-activity

我正在创建一个带有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语音时,我必须有一个活动(它将包含技术信息和通过菜单的地理定位)。 现在的问题是,如果我点击另一个声音,例如罗马路线,我有与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);



            }

请帮帮我!!!感谢

1 个答案:

答案 0 :(得分:0)

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

您可能想要将一些数据添加到用于启动Intent的{​​{1}},如下所示:

NuovaActivity