Problem in switching activities

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem in switching activities

Postby kitty » Thu Dec 03, 2009 4:19 pm

Hi all,

i've written one code for displaying clickable images in a Grid view. If one image is clicked, one new activity should be started. Instead of that i'm getting only single activity when i'm clicking any image in the Grid view. How do i set different activities for each image in the Grid view???.

Any heip will be highly appreciated.

Thanx and regards
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Postby qlimax » Thu Dec 03, 2009 6:42 pm

it would be nice to help you, but with your description of the problem, that's a bit difficult...
can you post some of your code?
maybe the part where you implement the clicklistener...

:?: bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Problem in switching activities

Postby kitty » Fri Dec 04, 2009 5:31 am

Hi qlimax,

Thanx for your quick reply. In my code i've written onclick listener for Gridview, Each grid in the Gridview has to start a new activity whenever i click that grid, as of now i assigned only one activity for that Gridview. How to assign different activities for each grid in the Grid view. My code is given below

Options.java

package com.SAI.filebrowser;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Options extends Activity
{
private Integer[] images={R.drawable.sms,R.drawable.mms,R.drawable.email2,R.drawable.contact,R.drawable.icon};
GridView gridview;

public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.options);

gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView arg0,View arg1,int arg2,long arg3)
{
Intent in=new Intent();
in.setClass(Options.this, AndroidFileBrowser.class);
startActivity(in);
}
});
}

public class ImageAdapter extends BaseAdapter
{

private Context context;

public ImageAdapter(Context context)
{
this.context = context;
}

public int getCount()
{
return images.length;
}

public Object getItem(int position)
{
return position;
}

public long getItemId(int position)
{
return position;
}

public View getView(int position, View convertView, ViewGroup parent)
{

ImageView iv;
if (convertView == null)
{
iv = new ImageView(context);
iv.setLayoutParams(new GridView.LayoutParams(100, 100));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
}
else
{
iv=(ImageView) convertView;
}

iv.setImageResource(images[position]);

return iv;
}

}
}

I will be waiting for your reply.

Thanx
Kitty
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Postby qlimax » Fri Dec 04, 2009 2:14 pm

try this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. gridview.setOnItemClickListener(new OnItemClickListener()
  2.  
  3.      {
  4.  
  5.       public void onItemClick(AdapterView arg0,View arg1,int arg2,long arg3)
  6.  
  7.           {
  8.  
  9.                                Intent in=new Intent();
  10.  
  11.                                 switch (arg2) { //arg2 is the position
  12.  
  13.                                 case 0:
  14.  
  15.                                         in.setClass(Options.this, AndroidFileBrowser.class);
  16.  
  17.                                         break;
  18.  
  19.                                
  20.  
  21.                                 case 1:
  22.  
  23.                                         in.setClass(Options.this, Abc.class);
  24.  
  25.                                         break;
  26.  
  27.                                        
  28.  
  29.                                 ...
  30.  
  31.  
  32.  
  33.                                 default:
  34.  
  35.                                         in.setClass(Options.this, DefaultActivity.class);
  36.  
  37.                                         break;
  38.  
  39.                                 }
  40.  
  41.                                
  42.  
  43.                                 startActivity(in);
  44.  
  45.            }
  46.  
  47.     });
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



let me know .... bye :wink:
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Switching activities

Postby kitty » Sat Dec 05, 2009 5:23 am

Hi qlimax,

Thanks for your reply. Its working, i already found a solution, i used getItemIdAtPosition() for getting the position of the item in the Gridview.

Any way Thanks.
kitty
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Dec 02, 2009 12:00 pm
Location: India

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests