2013-04-07 41 views
-1

我有一個令人困惑的問題,我有一個列表菜單,我想爲每個項目打開一個意圖,但我沒有單行字符串我不知道如何設置每個項目其意圖。 ps我有大部分的應用程序,我只是決定改變菜單,不能讓它工作。Java Android應用程序 - OnItemClickListener

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

} 

}; 

謝謝

+0

對不起忘了說位置的可能性。 「新聞/博客」鏈接到RSSItem.java「日曆」將鏈接到calender.java等等 – user2255522 2013-04-07 21:29:47

+0

看我的帖子,併發送給我你的反饋 – 2013-04-07 21:57:39

回答

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 
} 

的這裏面因爲一個事實,即它維持秩序,那麼你必須去到火所需的意圖

+0

作爲一個小建議,你可以使用命名常量而不是'0' ,'1'等 – 2013-04-07 22:03:25

+0

非常感謝你解決了你的解決方案後,工作的爐排謝謝你:) – user2255522 2013-04-07 23:02:09

+0

@ user2255522不客氣 – 2013-04-07 23:04:20