我有一个图片库。每次单击图库中的图像时,我都想打开其他活动。我在使用Onclicklisteners / onitemclicklisteners时遇到了困难。
这是我的代码。
public class MainActivity extends ActionBarActivity {
private Integer[] pics = { R.drawable.school, R.drawable.gradeview, R.drawable.history, R.drawable.schoolorg };
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
imageView = (ImageView) findViewById(R.id.imageView1);
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
//
// I DON'T KNOW WHAT TO PUT HERE
// I DON'T KNOW WHAT TO PUT HERE
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter{
private Context context;
int imageBackground;
public ImageAdapter(Context context){
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
imageView.setImageResource(pics[arg0]);
return imageView;
}
}
}
答案 0 :(得分:0)
打开其他活动,可以简化用户意图,
Intent intent = new Intent();
intent.setClass(getApplicationContext(), YOUR_NEW_ACTIVITY.class);
startActivity(intent);
但请确保您的新活动必须在清单中声明:
<activity android:name=".YOUR_NEW_ACTIVITY"></activity>
答案 1 :(得分:0)
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent =new Intent(MainActvity.this,ImageShow.class);//Class name to redirect
intent.putExtra("Image",pics[arg2]); //Image url to send to the next to show image
startActivity(intent);
this.finish();
}
});