I have used BaseAdapter for grid view, in getView method of BaseAdapter I am starting new activity using context of main activity, but when i tap on icon i getting message application not found for 1 sec and then new activity starts, in log cat I get error PakageManager$PakageNotFoundEception. Thanks in advance for your help.
public class ImageAdapter extends BaseAdapter{
private Context mContext;
ArrayList<Integer> imgid;
Uri uri;
public ImageAdapter(Context c, ArrayList<Integer> imgid2 ) {
super();
mContext = c;
imgid=imgid2;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return imgid.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
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.setClickable(true);
imageView.setPadding(8, 8, 8, 8);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("onClick","position ["+position+"]");
try {
final Intent intent= new Intent("settings");
intent.setClass(mContext, listAvtivity.class); //*******
if(imgid.get(position)==R.drawable.setting4){mContext.startActivity(intent);}
if(imgid.get(position)==R.drawable.google){uri = Uri.parse("http://www.google.com");}
if(imgid.get(position)==R.drawable.yahoo){uri = Uri.parse("http://www.yahoo.com");}
if(imgid.get(position)==R.drawable.skype){uri = Uri.parse("http://www.skype.com");}
Intent intent1 = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(intent1);
} catch (Exception e) {
// TODO: handle exception
Log.e("onClick Error: ",e.toString());
}
}
});
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imgid.get(position));
return imageView;
}
}
Don't use View.OnClickListener inside your getView method. Use OnItemClickListener and add it to your GridView to get notified when the user clicks on an item.
Related
i want when i click image from gridview, image send to new layout (Details Image)
this is MainActivity
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
i.putExtra("arg3", arg2);
startActivity(i);
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds;
}
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(95, 95));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(this.mContext)
.load(URL + position +".jpg")
// .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
.into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images }
in here i try get image from gridview, i try use intent, it's not work
Details Image
public class SingleViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Intent i = getIntent();
int position = i.getExtras().getInt("arg3");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.getItemViewType(position));}}
can anyone help me
Two points:
Pass the URL of the image you selected from MainActivity to SingleViewActivity.
Use ImageLoader, and do not modify the width/heigth otherwise the memory cache will miss.
Others the cache will do everything for you.
Sorry for my english. There is a mini-gallery, when you click on an image from the gallery it should (picture) to open in full screen. App just throws by pressing the image. what am I doing wrong?
MainActivity
public class MainAcTwo extends Activity {
#SuppressWarnings("deprecation")
Gallery gallery;
ImageView bigimage;
#SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two);
gallery=(Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
#TargetApi(Build.VERSION_CODES.HONEYCOMB) #SuppressLint("NewApi") public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
long imageId = ImageAdapter.ThumbsIds[position];
Intent fullScreenIntent = new Intent(v.getContext(), FullScreenImage.class);
fullScreenIntent.putExtra(MainAcTwo.class.getName(), imageId);
MainAcTwo.this.startActivity(fullScreenIntent);
}
});
}
}
ImageAdapter
public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
public ImageAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return ThumbsIds.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView=null;
if(convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(215, 200));
imageView.setPadding(8, 8, 8, 8);
}else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(ThumbsIds[position]);
return imageView;
}
public static Integer[] ThumbsIds={
R.drawable.abs_icla,
R.drawable.abs_dog,
R.drawable.abs_flow,
R.drawable.abs_neb,
R.drawable.abs_rad
};
}
FullScreenImage
public class FullScreenImage extends Activity {
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.full_image);
Intent intent = getIntent();
long imageId = (Long) intent.getExtras().get(FullScreenImage.class.getName());
ImageView imageView = (ImageView) findViewById(R.id.fullImage);
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
imageView.setImageResource((int) imageId);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
clean you project if it does not work
imageView.setLayoutParams( new ViewGroup.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
change it to
imageView.setLayoutParams( new LinearLayout.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));
I am new to android development. In my android activity I have a GridView and a Button. The GridView adapter class is displaying a set of icons in the GridView. What I want is, if the Button in the activity is clicked, the set of icons in the GridView should be replaced with another set of icons. And every Button click in the activity should switch the two sets of icons in the GridView.
So, how to do this using notifyDatasetChanged()? Please Help.
GridView Adapter class:
public class CustomAdapter extends BaseAdapter{
boolean imageSetChange = false;
// set 1
public Integer[] mThumbPics = {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15, R.drawable.pic16,
R.drawable.pic17, R.drawable.pic18,
R.drawable.pic19, R.drawable.pic20,
R.drawable.pic21
};
//set 2
public Integer[] mThumbEng = {
R.drawable.eng_pic1, R.drawable.eng_pic2,
R.drawable.eng_pic3, R.drawable.eng_pic4,
R.drawable.eng_pic5, R.drawable.eng_pic6,
R.drawable.eng_pic7, R.drawable.eng_pic8,
R.drawable.eng_pic9, R.drawable.eng_pic10,
R.drawable.eng_pic11, R.drawable.eng_pic12,
R.drawable.eng_pic13, R.drawable.eng_pic14,
R.drawable.eng_pic15, R.drawable.eng_pic16,
R.drawable.eng_pic17, R.drawable.eng_pic18,
R.drawable.eng_pic19, R.drawable.eng_pic20,
R.drawable.eng_pic21
};
private Context mContext;
View MyView;
ImageView imageView;
public CustomAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbEng.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbEng[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(imageSetChange==false)
{if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbEng[position]);
return MyView;
}
else{
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbUrdu[position]);
return MyView;
}
}
/** public void changeImages(boolean change){
this.imageSetChange = change;
cda.notifyDataSetChanged();
}
**/
}
Button onclick listener:
Button lang_change_btn = (Button) findViewById(R.id.button1);
lang_change_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
//cda.changeImages(true);
}
});
This is my simple adapter:
public class MainAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mText = {
R.string.main_team,
R.string.main_league,
R.string.main_economy,
R.string.main_arena,
R.string.main_staff,
R.string.main_team_up
};
public MainAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return mText.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button buttonView;
if (convertView == null) { // if it's not recycled, initialize some attributes
buttonView = new Button(mContext);
buttonView.setLayoutParams(new GridView.LayoutParams(85, 45));
//imageView.setScaleType(Button.ScaleType.CENTER_CROP);
buttonView.setPadding(4, 4, 4, 4);
} else {
buttonView = (Button) convertView;
}
buttonView.setText(mText[position]);
return buttonView;
}
}
Every button has to start its own intent.
I was thinking of using a switch case based on the position but it seems really "stupid" as solution.
I would create a new object, eg. ActivityLaunchButtonData to store the button Text and the button Intent.
Then I would have an array of ActivityLaunchButtonData objects.
Then in the getView method I'd have something like:
ActivityLaunchButtonData activityLaunchButtonData = activityLaunchButtonDataArray[position];
buttonView.setText(activityLaunchButtonData.getText());
buttonView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = activityLaunchButtonData.getIntent();
startActivity(intent);
}
});
If you know, in your adapter the intent to fire and the data to put in it you can try something like that:
public class MainAdapter extends BaseAdapter implements OnClickListener {
private Context mContext;
private Activity mActivity;
private Integer[] mText = {
R.string.main_team,
R.string.main_league,
R.string.main_economy,
R.string.main_arena,
R.string.main_staff,
R.string.main_team_up
};
public MainAdapter(Context c,Activity a) {
mContext = c;
mActivity = a;
}
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Button buttonView;
if (convertView == null) { // if it's not recycled, initialize some attributes
buttonView = new Button(mContext);
buttonView.setLayoutParams(new GridView.LayoutParams(85, 45));
//imageView.setScaleType(Button.ScaleType.CENTER_CROP);
buttonView.setPadding(4, 4, 4, 4);
} else {
buttonView = (Button) convertView;
}
buttonView.setText(mText[position]);
// For exemple a String, but setTag takes an object
buttonView.setTag("dataToSendThrewTheIntent");
buttonView.setOnClickListener(this);
return buttonView;
}
#Override
public void onClick(View v) {
String dataToSendThrewTheIntent = (String) v.getTag();
Intent i = new Intent(mContext, DestinationActivity.class);
i.putExtra("key", dataToSendThrewTheIntent);
mActivity.startActivity(i);
}
}
This method avoids multiple onClick listeners.
I have one GridView with 3 Column and 3 Rows I want to change Image when User Click any two Images.
for Example I Click First Row 1 and Column 3 Image and Secondly I Click on Row 3 and Column 2 show now i want to change this two Images like Swap the Image How is it Possible ?
public class MainActivity extends Activity {
/** Called when the activity is first created. */
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
int i=0; int j=0;
if( i != 0){
j=arg2;
System.out.println("First Click "+j);
}else{
i=arg2;
System.out.println("Second Click "+i);
}
im.getItem(arg2);
//im.changeImage();
Toast.makeText(MainActivity.this, ""+arg2, Toast.LENGTH_SHORT).show();
System.out.println("AdapterView "+arg0);
System.out.println("View "+arg1);
System.out.println("Integer "+arg2);
System.out.println("long "+arg3);
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
public void changeImage(){
iView.setImageResource(mThumbIds[5]);
}
}
Swaping the images in the GridView is very simple.What you have to do is
1* Store the cliked position,where you want to perform the swaping .
2* By using those two values perform the swap operation on mThumbIds array.
3* Finally invoke the notifyDataSetChanged() method on the Adapter object i.e im.notifyDataSetChanged();
public class MainActivity extends Activity {
/** Called when the activity is first created. */
int i=0;
int firstClick,secondClick;
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.gridviewmy);
gridView.setAdapter(new ImageAdapter(this));
final ImageAdapter im = new ImageAdapter(this);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub
i++;
if( i %2!=0){
firstClick=arg2;
}else{
secondClick=arg2;
Integer help=new Interger(mThumbIds[firstClick]);
mThumbIds[firstClick]=mThumbIds[secondClick];
mThumbIds[secondClick]=help;
notifyDataSetChanged();
System.out.println("Second Click "+i);
}
}
});
}
}
class ImageAdapter extends BaseAdapter{
private Context mContext;
ImageView iView;
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
System.out.println("Item Is :-"+mThumbIds[position].toString());
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
System.out.println("Geting Id of Item "+mThumbIds[position]);
if(iView != null){
iView.setImageResource(mThumbIds[0]);
Toast.makeText(mContext, "Call", Toast.LENGTH_SHORT).show();
}
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if( convertView == null){
iView = new ImageView(mContext);
iView.setLayoutParams(new GridView.LayoutParams(85, 85));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(8,8,8,8);
}else{
iView = (ImageView)convertView;
}
iView.setImageResource(mThumbIds[position]);
return iView;
}
private Integer[] mThumbIds = {
R.drawable.a_bhaibij, R.drawable.a_dashera, R.drawable.a_dipawali,
R.drawable.a_gandhi, R.drawable.a_holi, R.drawable.a_indepe,
R.drawable.a_janmastmi, R.drawable.a_kite, R.drawable.a_newyear
};
}
I think this may solve you problem.
All the best.
Also do the following for updating the grid view images to complete the swap operation:
im.notifyDataSetChanged();
gridView.setAdapter(im);
gridView.invalidateViews()
notifyDataSetChanged(); did not work for me. eclipse gave an error.
so instead of searching for the real solution, if there is one, I just reloaded the java page.
Of course I am saving the state of the images in the gridview (adapter) in internal storage in a file named graphics. So on reload of the java page it repaints with correct images.
It works.