how to start new activity via gridview onitemclick?

删除回忆录丶 提交于 2019-12-25 19:29:24

问题


i want to open new activity on itemclick listner in gridview.like this. Click position 1(india) ->open activity 1.Click position 2(brazil)->open activity 2.Click position 3(canada)->open activity 3. how to possible it? This is my code.

   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);



}
}

Thanks !!


回答1:


You can open activity using intent based on position

 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();
                }
            });



回答2:


Starting any activity is exactly same in every situation. You just create an intent and supply it to startActivity() method.

@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
         }
    }



回答3:


In your grid item click listener start the 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);
        }
    });

and in your detail activity get data from intent like this

in onCreate()

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

in your details activity you can check country selected and set data accordingly

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



回答4:


you can try with country name also

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);



回答5:


Use this code -

  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;
                }
            }
        });

Here's you can see more explained code.

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




回答6:


you can compare with position.

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);



回答7:


you need to add this code in your OnItemClickListener there are two ways to do this. If you have the same layout but different content then you can only create one activity and put extras and change the content. If you have different design for layout, then use this,

 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);


来源:https://stackoverflow.com/questions/41561813/how-to-start-new-activity-via-gridview-onitemclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!