-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 に答える