如何在GridView onclick中调用另一个Activity

时间:2012-10-18 08:04:37

标签: android

String[] countries = new String[] {"India","Pakistan"};    
int[] flags = new int[]{R.drawable.india, R.drawable.pakistan};    
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", countries[i]);    
    hm.put("flag", Integer.toString(flags[i]) );    
    aList.add(hm);
}    
String[] from = { "flag","txt"};    
int[] to = { R.id.flag,R.id.txt};    
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridview_layout, from, to);    
GridView gridView = (GridView) findViewById(R.id.gridview);    
gridView.setAdapter(adapter); 

这是我的网格视图。当鼠标点击India Image时,我想调用另一个Activity。点击巴基斯坦时再拨打另一项活动。我想用Intent做到这一点。怎么样?

2 个答案:

答案 0 :(得分:0)

实现OnItemSelected接口并在那里处理click事件。您可以检查单击的项目并根据该项目激发不同的意图。

请参阅http://developer.android.com/reference/android/widget/AdapterView.OnItemSelectedListener.html#onItemSelected

答案 1 :(得分:0)

请为此添加Gridview onitemclicklistener代码。

gridView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        if(countries[arg2].equals("India")){
            Intent mIndia=new Intent(this, India_Activity.class);
            startActivity(mIndia);
        } else if(countries[arg2].equals("Pakistan")){
            Intent mPakistan=new Intent(this, Pakistan_Activity.class);
            startActivity(mPakistan);
        }
    }
});