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
Related
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...
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!
I have created listview in Android to show my records. And records are shown as well. Now I want to click on particular record from that listview to show/view particular record. And that record must show another layout for that profile. How to achieve it?
Create a onItemClick() event for the listview. Then, when you click on an item, a new intent will be created that will start a child activity that will display the particular record.
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(MyActivity.this, OtherActivity.class);
startActivity(launchActivity);
}
});
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.
First of all I want to know if this is possible. Essentially I have a listview that is being populated programatically with a custom list item view and a custom adapter. This works like a charm but the problem is that I have to sort this list in a specific way once it is populated. The values that are being put into the list are stored in String arrays. The problem is that when I sort the listview it changes the index of where I thought the values would be stored. I want to start a new activity with certain values found in the list view. Is this possible? I could take a string from the list item and can look it up in my arrays storing the values when that item is clicked. I can then start a new intent with extras so that I can pass this new data to the new activity. So, is it possible to extract a string from a list item?
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3){
Object o=lv.getItemAtPosition(position);
UtilityClass u=(UtilityClass)o;
String s=u.getMyData();
Intent edit = new Intent(context, AnotherActivity.class);
edit.putExtra("your_identifier", Integer.toString(position));
startActivity(intent);
}});
If u have implemented onItemClickListner / onItemSelectListner then you can get a callback onItemClicked()/ onItemSelected() from there using the Adapter get the item from selected position. and the same can be sent to another activity using a bundle/extra