从GridView中单击的每个项目打开一个新活动

时间:2014-03-18 07:55:34

标签: gridview android-intent android-activity

在此程序中,列表显示为网格。当我点击列表时,所选项目将打开一个新活动。但它没有正常工作。仅显示GridItem& Textviewing单击的项目。单击不打开新活动/意图

这就是我的......

GridViewDemo.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;


public class GridViewDemo extends Activity implements AdapterView.OnItemClickListener {

    TextView selection;
String[] items = { "One", "Two", "Three };

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    selection = (TextView) findViewById(R.id.selection);

            //Intent i = new Intent(this, One.class) something will declare here

    GridView gv = (GridView) findViewById(R.id.grid);

    ArrayAdapter<String> aa = new ArrayAdapter<String>(
            this,
            android.R.layout.simple_list_item_1, 
            items );

    gv.setAdapter(aa);
    gv.setOnItemClickListener(this);
    }

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

//i want to start a new intent here accordong to the possition (One, two, Three.class)
//i don't need the textview
    selection.setText(items[position]);

    }

}

这将有助于

protected void onListItemClick(ListView l, View v, int position, long id) {     
super.onListItemClick(l, v, position, id);

try {
Class classe = Class.forName("com.google.GridViewDemo." + items[position]);
Intent i = new Intent(this, classe);
startActivity(i);

2 个答案:

答案 0 :(得分:0)

1)为您的活动实施onItemClickListener。

2)然后添加未实现的方法。

3)在onItemClick()方法中你会得到像这样的东西:

 @Override
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
    // TODO Auto-generated method stub



       Intent i = new Intent(getApplicationContext(),DestinationActivity.class);
       startActivity(i);


    }

答案 1 :(得分:0)

GridView gridView = (GridView) findViewById(R.id.gridView);

        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {               
                Intent intent = new Intent(Gallery.this, GalleryView.class);

                startActivity(intent);
            }
        });