我的代码:
public class ViewsActivity extends Activity implements OnClickListener
{
//
private Integer[] ImgIds = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4 };
private LinearLayout mLinearLayout;
private Gallery g;
private MediaPlayer mp;
private ImageView i;
private Thread th;
private Runnable viewUpdate;
private int m_index = 0;
private OnClickListener mImageListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
setContentView(R.layout.main);
g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(v.getContext()));
g.setOnItemClickListener(mGalleryClickListener);
g.setOnItemSelectedListener(mGallerySelectedListener);
}
};
private OnItemSelectedListener mGallerySelectedListener = new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng)
{
String descr = "";
switch(i)
{
case 0:
descr = "scena1";
break;
case 1:
descr = "scena2";
break;
case 2:
descr = "scena3";
break;
case 3:
descr = "scena4";
break;
case 4:
descr = "scena5";
break;
case 5:
descr = "scena6";
break;
case 6:
descr = "scena7";
break;
case 7:
descr = "scena8";
break;
case 8:
descr = "scena9";
break;
case 9:
descr = "scena10";
break;
}
Toast.makeText(ViewsActivity.this, descr, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView arg0)
{
}
};
private OnItemClickListener mGalleryClickListener = new OnItemClickListener()
{
public void onItemClick(AdapterView parent, View v, int position, long id)
{
switch(position)
{
case 0:
i.setImageResource(R.drawable.pic1);
break;
case 1:
i.setImageResource(R.drawable.pic2);
break;
case 2:
i.setImageResource(R.drawable.pic3);
break;
case 3:
i.setImageResource(R.drawable.pic4);
break;
case 4:
i.setImageResource(R.drawable.pic5);
break;
case 5:
i.setImageResource(R.drawable.pic6);
break;
case 6:
i.setImageResource(R.drawable.pic7);
break;
case 7:
i.setImageResource(R.drawable.pic8);
break;
case 8:
i.setImageResource(R.drawable.pic9);
break;
case 9:
i.setImageResource(R.drawable.pic10);
break;
}
setContentView(mLinearLayout);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp = MediaPlayer.create(this, R.raw.song);
mp.start();
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView parent, View v, int position, long id) {
if (position >= ImgIds.length) {
position = position % ImgIds.length;
}
// Toast.makeText(this, ""+position, Toast.LENGTH_SHORT).show();
}
});
}
//
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return Integer.MAX_VALUE;
}
public Object getItem(int position) {
if (position >= ImgIds.length) {
position = position % ImgIds.length;
}
return position;
}
public long getItemId(int position) {
if (position >= ImgIds.length) {
position = position % ImgIds.length;
}
return position;
}
public View getView1(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
if (position >= ImgIds.length) {
position = position % ImgIds.length;
}
i.setImageResource(ImgIds[position]);
i.setLayoutParams(new LayoutParams(80, 80));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
public int checkPosition(int position) {
if (position >= ImgIds.length) {
position = position % ImgIds.length;
}
return position;
final Button buttonavanti = (Button) findViewById(R.id.Avanti);
buttonavanti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
g.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
}
});
final Button buttonindietro = (Button) findViewById(R.id.Indietro);
buttonindietro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
g.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, null);
}
});
final Button buttonexit = (Button) findViewById(R.id.Exit);
buttonexit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
mLinearLayout = new LinearLayout(this);
i = new ImageView(this);
i.setImageResource(R.drawable.pic1);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mLinearLayout.addView(i);
mLinearLayout.setOnClickListener(mImageListener);
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
我在mLinearLayout = new LinearLayout(this)
和i = new ImageView(this)
时遇到“构造函数未定义”错误。
答案 0 :(得分:5)
您似乎是从ImageAdapter调用LinearLayout(Context context)
,因此无法使用this
。您需要传递上下文并且ImageAdapter中的this
不引用上下文。使用本地声明的变量mContext
mLinearLayout = new LinearLayout(mContext)
i = new ImageView(mContext).
答案 1 :(得分:1)
这将有效..
mLinearLayout = new LinearLayout(ViewsActivity.this);
答案 2 :(得分:0)
这与仅使用参数layout_width和layout_height创建视图或布局相同。
您需要调用构造函数,该构造函数还将LayoutParams作为参数并为其提供数据。