-1

我有一个令人困惑的问题我有一个列表菜单,我想像往常一样为每个项目打开一个意图,但我没有单行字符串我不确定如何将每个项目设置为它的意图。ps 我制作了大部分应用程序,我只是决定更改菜单,但无法正常工作。

package com.test.thenewboston;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.R.menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Menu extends Activity {


String[] countries = new String[] {
        "News / Blog",
        "Calendar",
        "Results",
        "Standings",
        "Photo",
        "Videos",
        "About us",

};

// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
        R.drawable.rssreader,
        R.drawable.calendar2,
        R.drawable.results2,
        R.drawable.standings2,
        R.drawable.photo2,
        R.drawable.video2,
        R.drawable.about2,

};

// Array of strings to store currencies
String[] currency = new String[]{
    "  Check out the new blog by our team of f1 fans !",
    "  Race calendar with dates and times !",
    "  The latest results from this seasons 2013 Championship",
    "  The Current 2013 Standings",
    "  Latest Photos from us and our bloggers",
    "  This is still underdevolpment but if you have any videos contact us :)",
    "  All our contact information and extra info about out team.",

};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        

    // Each row in the list stores country name, currency and flag
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();        

    for(int i=0;i<7;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put("txt", " | " + countries[i] + " | ");
        hm.put("cur","" + currency[i]);
        hm.put("flag", Integer.toString(flags[i]) );            
        aList.add(hm);        
    }

    // Keys used in Hashmap
    String[] from = { "flag","txt","cur" };

    // Ids of views in listview_layout
    int[] to = { R.id.flag,R.id.txt,R.id.cur};        

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from,              to);

    // Getting a reference to listview of main.xml layout file
    ListView listView = ( ListView ) findViewById(R.id.listview);

    // Setting the adapter to the listView
    listView.setAdapter(adapter);       

    // Item Click Listener for the listview
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
             Intent countrys = new Intent(adapter.toString());
             startActivityForResult(countrys, 0);


}

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mainmenu, (android.view.Menu) menu);
    return true;
}        });

}

}; 

谢谢

4

2 回答 2

2

把这个放在setOnItemClickListener()方法里面

listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

    if (position==0) {
            Intent news = new Intent(Menu.this ,RSSItem.class);
            startActivity(news);
    }else if (position==1) {
            Intent calendar = new Intent(Menu.this ,calender.class);
            startActivity(news);
    }
 // and so on
}

因为它维持的顺序,所以你有可能越过这个位置来触发你想要的意图

于 2013-04-07T21:55:00.763 回答
1

里面

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    HashMap<String,String> aMap = (HashMap<String,String>)parent.getItemAtPosition(position);
}

http://developer.android.com/reference/android/widget/AdapterView.html#getOnItemClickListener()

也许这有帮助?

于 2013-04-07T21:47:59.690 回答