如何通过 gridview onitemclick 启动新的 activity?

how to start new activity via gridview onitemclick?

我想在 gridview.like 中打开新的 activity on itemclick listner。 点击位置 1(印度)->open activity 1.Click position 2(brazil)->open activity 2.Click position 3(canada)->open activity 3. 怎么可能呢? 这是我的代码。

   android.widget.AdapterView.OnItemClickListener; 
public class GridViewExampleActivity extends Activity {
 /** Called when the activity is first created. */

private GridviewAdapter mAdapter;
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;

private GridView gridView;

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

    prepareList();

    // prepared arraylist and passed it to the Adapter class
    mAdapter = new GridviewAdapter(this,listCountry, listFlag);

    // Set custom adapter to gridview
    gridView = (GridView) findViewById(R.id.gridView1);
    gridView.setAdapter(mAdapter);

    // Implement On Item click listener
    gridView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
        }
    });

}

public void prepareList()
{
      listCountry = new ArrayList<String>();

      listCountry.add("india");
      listCountry.add("Brazil");
      listCountry.add("Canada");
      listCountry.add("China");


      listFlag = new ArrayList<Integer>();

      listFlag.add(R.drawable.india);
      listFlag.add(R.drawable.brazil);
      listFlag.add(R.drawable.canada);
      listFlag.add(R.drawable.china);



}
}

谢谢!!

您可以根据位置

使用意图打开 activity
 gridView.setOnItemClickListener(new OnItemClickListener()
            {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                        long arg3) {
                    if(position==1) {
                        Intent intent = new Intent(GridViewExampleActivity.this, IndiaActivity.class);
                        startActivity(intent);
                    }
                    else if(position==2)
                    {
                        Intent intent = new Intent(GridViewExampleActivity.this, BrazilActivity.class);
                        startActivity(intent);

                    }
                    Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
                }
            });

您需要在 OnItemClickListener 中添加此代码 有两种方法可以做到这一点。如果布局相同但内容不同,则只能创建一个 activity 并添加额外内容并更改内容。如果你有不同的布局设计,那么使用这个,

 switch (position) {
 case 0:
   Intent intent = new Intent(GridViewExampleActivity.this,ActivityOne.class);
  break;
 case 1:
  Intent intent = new Intent(GridViewExampleActivity.this,ActivityTwo.class);
  break;
}

startActivity(intent);

你可以比较排名。

switch (listCountry.get(position)) {
   case 0:
      Intent intent = new Intent(GridViewExampleActivity.this,IndiaActivity.class);
      break;
   case 1:
      intent = new Intent(GridViewExampleActivity.this,BrazilActivity.class);
      break;
}
startActivity(intent);

开始任何 activity 在任何情况下都完全相同。您只需创建一个意图并将其提供给 startActivity() 方法。

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        if(position == 1) {
            // start activity for India
            startActivity(new Intent(GridViewExampleActivity.this, InidaActivity.class));
         } else if(position == 2) {
            // start activity for Brazil
         }
    }

在您的网格项目中点击侦听器启动 activity

gridView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(GridViewExampleActivity.this , YourDetailsActivity.class)
            intent.putExtra("country", listCountry.get(position);
            intent.putExtra("flag", listFlag.get(position);
            startActivity(intent);
        }
    });

在您的详细信息中activity从这样的意图中获取数据

在 onCreate() 中

country = (String)getIntent().getExtra().get("country");
flag = (int)getIntent().getExtra().get("flag");

在您的详细信息中 activity 您可以检查所选国家并相应地设置数据

if(country.equals("india")){
    // Show data related to india
}

你也可以试试国家名

switch (listCountry.get(position)) {


 case "India":
      Intent intent = new Intent(GridViewExampleActivity.this,IndiaActivity.class);
      break;
   case "Brazil":
      Intent intent = new Intent(GridViewExampleActivity.this,BrazilActivity.class);
      break;
   }
startActivity(intent);

使用此代码 -

  gridview.setOnItemClickListener(new OnItemClickListener() {

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

    switch (position) {

    case 0:
                   //Use some different intent here
    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;
                }
            }
        });

这里有更多解释代码。

http://www.androidbegin.com/tutorial/android-simple-gridview-tutorial/