Android: Clicking children individually within a GridView - android

This is in the initial Activity when my app starts up. The gridview contains six click able icons that i want to all go to different activities. I have aboslutely no problem starting up a new activity for another thing to do in my app. I created one activity just to work with initially, a 'contact us' form. I want only one of these icons to go to that activity, however i can't find a way to make it so the onItemClickListener callback registers individual clicks for each icon and launches the appropriate activity; currently, no matter which icon i click, they all go to the same activity. See below for my code:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
Intent myIntent = new Intent();
myIntent.setClassName("com.beneast.main", "com.beneast.main.ContactUs");
startActivity(myIntent);
};
});
I am a bit of a noob at doing this as you can plainly see, this app is getting developed as i learn more stuff. But thanks for any help.

You need to make use of the int position parameter in the onitemClick method. The position parameter is the position of the icon in the gridview which has been clicked. So i would use a switch-case statement for your need as follows:
gridview.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
switch(position)
{
case 0:
Intent myIntent = new Intent();
myIntent.setClassName("com.beneast.main", "com.beneast.main.ContactUs");
startActivity(myIntent);
break;
case 1:
Intent myIntent = new Intent();
myIntent.setClassName(X, Y);
startActivity(myIntent);
break;
}
};
});
You would continue this to cover all possible cases i.e. all icon positions in the gridview.

Related

Get all ListView items?

I want to get all the items in a list view that way when each one is clicked they will open the second activity?
Here I can get a specific list view item and use an if statement to say if this is clicked open/launch the second activity but I want to be able to say if any list view item is clicked open/launch the second activity how do I do that I know it sounds simple??
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
when I remove the if statement it keeps crashing logcat states:"RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list" am using :android:id="#android:id/list" my activity also extends ListActivity? I dont't get it
If I understand your question:
but I want to be able to say if any list view item is clicked open/launch the second activity how do I do that I know it sounds simple??
Just remove the if block like this:
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
And your second activity will be opened on any item click.
Hope this helps...

Android GridView Item Click Event Handler Inside ExpandableListView

I have an Android GridView inside an ExpandableListView. All data is appearing fine but I can not find a way to handle GridView Item Click event. I just want to open up another Intent upon clicking on a grid's cell and pass a value to the intent.
Is it possible?
Thanks.
YourGrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(this,
MyNewActivity.class);
startActivity(intent);
}
});
Make sure in your GridLayout there is no clickable content like buttons otherwise they'll fired instead of this
grid.setOnChildClickListener();
may solve your problem!

Implementing viewpager in a list

How to implement an ExpandableList in a ViewPager in Android?
My question is the exact opposite of the above question, I want to display a list view (preferably an expandable one) and when an item is clicked I want display a bunch of images (different ones for each item of course).
P.S: Please dont tell me to launch a different activity for each list item.
This is some possible guidance not solution. (I am not exactly clear on what the problem is)
I would probably just create one activity. Each time you get a click on the listview it creates a intent with some stored flags. Then you start the activity with that intent. Unpackage the intent to find out what you want you activity to do.
private OnItemClickListener itemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(this, myclass.class);
intent.putExtra(KEY, v.getId())
intent.putExtra(KEY2, position)
intent.putExtra(KEY3, id)
startActivity(intent);
}
};
And then get the intents flags in the new activity onCreate()
Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
resId = intent.getIntExtra(KEY, 0);
...
super.setTitle(title);
Not sure if that what you were looking for hope it helps

How can i start a new activity within an Activity?

For example i have a video gallery. I scroll to a certain video click on it and a whole new activity starts. Whats the most efficient way to code this?
If you have a list, I'm sure you are getting a hold of it in the code and assigning an Adapter. You can define what happens (starting an activity) by defining setOnItemClickListener() as such:
final ListView list = (ListView)findViewById(R.id.myVideoList);
MyVideoAdapter adapter = new MyVideAdapter(this);
list.setAdapter(adapter);
list.setOnItemClickListener(new ListView.OnItemClickListener(){
public void onItemClick(AdapterView<?> av, View v, int position, long id){
MyVideoObject video = (MyVideoObject)av.getItemAtPosition(position);
Intent intent = new Intent(CurrentActivity.this, SelectedVideo.class);
intent.putExtra("video_id, video.getID());
startActivity(intent);
}
});
All you're doing above is to initializing the list and assigning what happens when the user clicks on the list item.
When a click happens, you're getting a hold of the video item (however you're juggling them between activities) and passing perhaps its ID to the new Activity you wanna launch. The new activity (SelectedVideo.class) can receive the ID in onCreate and perhaps play the video.
Hope this helps,
-serkan

android list view clickable problem

i have this customized list. each row contains an image and two lines of text one below the other. i want to open a new activity when any list item is clicked. but i am not able to do so, even after implementing the setOnItemClickListener(). please correct me if i am wrong. the below is the code for the list.
PS: This is an normal activity and not list activity.
l1.setAdapter(new EfficientAdapter(this,eventTitleArray,eventDateArray,eventImageLinkArray));
//l1 = getListView();
l1.setClickable(true);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
Bundle b = new Bundle();
b.putString("event", eventTitleArray[position]);
intent.putExtras(bundle);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Opening detailed view for:"+eventTitleArray[position], Toast.LENGTH_SHORT).show();
}
});
Please have a look whether the row layout has any items which are focusable. If an ListView Item contains focusable children, the Listview Handler will not be fired.
I think there is a bug in the SDK that prevents the onItemClickListeners from firing when there are focusable views in the View of your items.
So you should try to do a setFocusable(false) on all the Views of your items.
The problem is described here

Categories

Resources