I'm a new programmer and I need a way to start a new activity when an image is clicked in grid view. Everything I've tried doesn't work. Do you have any ideas? I need each Image to load a different activity. I have the grid working but just need it to change activity.
我是一名新程序员,在网格视图中单击图像时,我需要一种方法来启动新活动。我尝试过的一切都行不通。你有什么想法?我需要每个图像加载不同的活动。我有网格工作,但只是需要它来改变活动。
Java File:
Java文件:
package your.pacage.names;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
}
2 个解决方案
#1
6
in the given code you aren't setting any kind of onItemClickListener (on the grid) or View.OnClickListener on the individual imageViews... You need to do one or the other for anything to happen when you press on one of the grid view items.
在给定的代码中,您没有在单个imageViews上设置任何类型的onItemClickListener(在网格上)或View.OnClickListener ...当您按下其中一个网格视图时,您需要执行一个或另一个操作项目。
To start a new activity from an activity: startActivity(new Intent(YourActivity.this, TargetActivity.class))
从活动开始新活动:startActivity(new Intent(YourActivity.this,TargetActivity.class))
All together do the following in onCreate:
在onCreate*同执行以下操作:
GridView yourGridView = (GridView) findViewById(R.id.your_grid_view_id);
yourGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
Class targetActivity = getTargetActivityForPosition(position);
YourActivity.this.startActivity(new Intent(YourActivity.this, TargetActivity.class));
}
}
#2
0
sorry, here i ask my question, i have five new activity and set five different image related to five activists. so how can i set onClickItemListener to open new activity
对不起,我在这里问我的问题,我有五个新的活动,并设置五个与五个活动家有关的不同形象。那么如何设置onClickItemListener来打开新活动
#1
6
in the given code you aren't setting any kind of onItemClickListener (on the grid) or View.OnClickListener on the individual imageViews... You need to do one or the other for anything to happen when you press on one of the grid view items.
在给定的代码中,您没有在单个imageViews上设置任何类型的onItemClickListener(在网格上)或View.OnClickListener ...当您按下其中一个网格视图时,您需要执行一个或另一个操作项目。
To start a new activity from an activity: startActivity(new Intent(YourActivity.this, TargetActivity.class))
从活动开始新活动:startActivity(new Intent(YourActivity.this,TargetActivity.class))
All together do the following in onCreate:
在onCreate*同执行以下操作:
GridView yourGridView = (GridView) findViewById(R.id.your_grid_view_id);
yourGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l){
Class targetActivity = getTargetActivityForPosition(position);
YourActivity.this.startActivity(new Intent(YourActivity.this, TargetActivity.class));
}
}
#2
0
sorry, here i ask my question, i have five new activity and set five different image related to five activists. so how can i set onClickItemListener to open new activity
对不起,我在这里问我的问题,我有五个新的活动,并设置五个与五个活动家有关的不同形象。那么如何设置onClickItemListener来打开新活动