0

我創建一個菜單,一個ListView那裏有不同的類型學路線的分工:歷史,自然等副不同的聲音的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); 



      } 

請幫幫我!謝謝

回答

0

當您開始活動時,您不會告訴活動有關用戶選擇的內容。 NuovaActivity如何知道用戶點擊了什麼,以便它可以顯示正確的信息?

你可能想一些數據添加到Intent您用來啓動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); 
}