如何从GridView调用另一个活动?

时间:2017-12-08 15:26:50

标签: android gridview

我在我的应用中有gridview。我有6件物品。我想在点击项目时调用另一个动作。我该怎么做?这是我的代码。帮帮我吧 现在,当我点击项目时,我可以用吐司显示消息。但是我想要另外一个活动帮助我。

public class GridViewImageTextActivity extends AppCompatActivity {

    GridView androidGridView;

    String[] gridViewString = {
            "Hesapla", "Geçmiş", "Paylaş", "Geribildirim", "Sıralama", "Beğen",


    } ;
    int[] gridViewImageId = {
            R.drawable.hesap, R.drawable.gecmis, R.drawable.paylas, R.drawable.geribildirim, R.drawable.siralama, R.drawable.begen


    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view_image_text);

        CustomGridViewActivity adapterViewAndroid = new CustomGridViewActivity(GridViewImageTextActivity.this, gridViewString, gridViewImageId);
        androidGridView=(GridView)findViewById(R.id.grid_view_image_text);
        androidGridView.setAdapter(adapterViewAndroid);
        androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int i, long id) {
                Toast.makeText(GridViewImageTextActivity.this, "GridView Item: " + gridViewString[+i], Toast.LENGTH_LONG).show();
            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

您必须创建另一个活动的类,将此活动添加到AndroidManifest.xml文件并从这样开始:

@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
    Context context = GridViewImageTextActivity.this;
    Intent intent = null;
        switch (position) {
            case 0:
                intent = new Intent(context, FirstActivity.class);
                startActivity(intent);
                return;
            case 1:
                intent = new Intent(context, SecondActivity.class);
                startActivity(intent);
                return;
            case 2:
                // start another activity
                return;
            // and so on
        }
}

答案 1 :(得分:0)

  1. 首先为每项活动创建6个活动及其布局。
  2. 为每个6项指定ID,如button1,button2,button3等
  3. 将此切换案例声明替换为Toast消息行
  4. 以下是前3个项目的示例代码

    swithc(View v){case R.id.button1:startActivity(new Intent(GridViewImageTextActivity.this,ActivityB.clsss));break;case R.id.button2:startActivity(new Intent(GridViewImageTextActivity.this,ActivityC.clsss));break;case R.id.button3:startActivity(new Intent(GridViewImageTextActivity.this,ActivityD.clsss));break;}