2012-09-30 18 views
1

我使用Android中的簡單適配器使圖像和文本列表視圖。但我累了讓OnClickListener使用意圖切換其他活動。例如,我點擊印度,活動切換到IndiaActivity.java等...請幫助我..謝謝! (對不起不好英語)使用簡單的適配器在列表視圖中設置帶有圖片和文字的OnClickListener?

我的代碼:

public class MainActivity extends Activity { 

    // Array of strings storing country names 
    String[] countries = new String[] { 
     "India", 
     "Pakistan", 
     "Sri Lanka", 
     "China", 
     "Bangladesh", 
     "Nepal", 
     "Afghanistan", 
     "North Korea", 
     "South Korea", 
     "Japan" 
    }; 

    // Array of integers points to images stored in /res/drawable-ldpi/ 
    int[] flags = new int[]{ 
     R.drawable.india, 
     R.drawable.pakistan, 
     R.drawable.srilanka, 
     R.drawable.china, 
     R.drawable.bangladesh, 
     R.drawable.nepal, 
     R.drawable.afghanistan, 
     R.drawable.nkorea, 
     R.drawable.skorea, 
     R.drawable.japan 
    }; 

    // Array of strings to store currencies 
    String[] currency = new String[]{ 
     "Indian Rupee", 
     "Pakistani Rupee", 
     "Sri Lankan Rupee", 
     "Renminbi", 
     "Bangladeshi Taka", 
     "Nepalese Rupee", 
     "Afghani", 
     "North Korean Won", 
     "South Korean Won", 
     "Japanese Yen" 
    }; 

    /** 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<10;i++){ 
      HashMap<String, String> hm = new HashMap<String,String>(); 
      hm.put("txt", "Country : " + countries[i]); 
      hm.put("cur","Currency : " + 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); 
    } 

} 

回答

1
listView.setOnItemClickListener(new OnItemClickListener() { 

    public void onItemClick(AdapterView adapterView, View view, int position, long id) { 
    SimpleAdapter adapter = (SimpleAdapter) adapterView.getAdapter(); 
    ListView currentLv = (ListView) view; 

    Object item = adapter.getItem(position); 
    //Do some more stuff here and launch new activity 


    } 
}); 
+0

謝謝!但是如果條件選擇將要運行的活動,你可以舉一個例子來實現嗎? –

+0

對不起,哥們請探索然後問..只需要使用這個項目(對象)來匹配各自的鑄造與字符串和各自的對象 –

+0

好吧@Arpit,對不起,很多謝謝4你的答案..我可以探索你的答案.. :) –

相關問題