package mediology.android.testproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class TestProjectActivity extends Activity {
private static final String TAG = "TestProjectActivity";
TestProjectActivity _instance;
GridView gridView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
_instance = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView=(GridView)findViewById(R.id.gvtest);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
Toast.makeText( _instance , "Main Activity" + position, Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
}
});
}
}
package mediology.android.testproject;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView imageView;
private static final String TAG="ImageAdapter";
public ImageAdapter(Context c) {
mContext=c;
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
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;
}
Log.d(TAG,"return image view="+imageView);
// TODO Auto-generated method stub
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.ic_launcher,
R.drawable.images,
R.drawable.images10,
R.drawable.images23,
R.drawable.images56,
R.drawable.images67,
R.drawable.images7,
R.drawable.images89,
R.drawable.images9,
R.drawable.images90
};
}
No comments:
Post a Comment