我怎么能 - 通过GridView项目开始意图点击" (不是图像)

时间:2014-03-17 15:15:57

标签: gridview android-intent android-activity

我想启动保留在src下的每个意图。它们被命名为One.javaTwo.java等等。

这是我的java代码,但在点击GridView项目后,它会打开相同的Intent窗口,而不是One.javaTwo.java活动。

  • MyActivity.java:

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.GridView;
    public class MyActivity extends Activity {  
    
    static final String[] numbers = new String[] { "One", "Two", "Three", "Four", "Five", "Six",  "Seven"  };
    
    @Override
        public void onCreate(Bundle savedInstanceState) {      
            super.onCreate(savedInstanceState);
            setContentView(R.layout.myactivity);
    
            GridView gv = (GridView) findViewById(R.id.gridView1);
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers);
    
            gv.setAdapter(adapter);
            gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,int position, long id) {
                    Intent i = new Intent(getApplicationContext(), MyActivity.class);
                    i.putExtra("id", position);
                    startActivity(i);
                }
            });
        }
    }
    

这会有所帮助:

http://www.mkyong.com/android/android-gridview-example/

Android Gridview - Need to open different intents

1 个答案:

答案 0 :(得分:4)

以下是如何为网格视图中的每个位置启动意图的示例:

gv.setOnItemClickListener(new OnItemClickListener() {


            public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

                switch (position) {
                case 0:

                    Intent i = new Intent(getApplicationContext(), MyActivity.class);
                    i.putExtra("id", position);
                    startActivity(i);
                    break;

                case 1:
                    //Use some different intent here
                    Intent j = new Intent(getApplicationContext(), MyActivity.class);
                    j.putExtra("id", position);
                    startActivity(j);
                    break;
                case 2:
                    //Use some different intent here
                    Intent k = new Intent(getApplicationContext(), MyActivity.class);
                    k.putExtra("id", position);
                    startActivity(k);
                    break;
                case 3:
                    //Use some different intent here
                    Intent l = new Intent(getApplicationContext(), MyActivity.class);
                    l.putExtra("id", position);
                    startActivity(l);
                    break;

                default:
                    break;
                }



            }
            });

我希望这就是你要找的东西。