我想在gridview.like中打开itemclick listner上的新活动 。 点击位置1(印度) - >打开活动1.点击位置2(巴西) - >打开活动2.点击位置3(加拿大) - >打开活动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);
}
}
谢谢!
答案 0 :(得分:2)
您可以使用基于位置的意图打开活动
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();
}
});
答案 1 :(得分:1)
您需要在OnItemClickListener中添加此代码 有两种方法可以做到这一点。如果您具有相同的布局但内容不同,那么您只能创建一个活动并添加额外内容并更改内容。如果你有不同的布局设计,那么使用它,
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);
答案 2 :(得分:1)
你可以与位置进行比较。
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);
答案 3 :(得分:1)
在任何情况下,开始任何活动都是完全相同的。您只需创建一个intent并将其提供给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
}
}
答案 4 :(得分:1)
在您的网格项中,单击侦听器启动活动
-D__STDC_FORMAT_MACROS
并在您的详细活动中从此意图中获取数据
onCreate()中的
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);
}
});
在您的详细信息活动中,您可以检查所选国家/地区并相应地设置数据
country = (String)getIntent().getExtra().get("country");
flag = (int)getIntent().getExtra().get("flag");
答案 5 :(得分:1)
您也可以尝试使用国家/地区名称
if(country.equals("india")){
// Show data related to india
}
答案 6 :(得分:1)
使用此代码 -
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/