I have an application that primarily uses two activities, we'll call the first ListActivity as it contains a ListView and the other we'll call InfoActivity. When an item is clicked on the ListView the InfoActivity should be made active using a sliding transition, which I've implemented as follows
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//store the id of the item that was clicked
PlacesHelper.tappedId = position;
Intent i = new Intent(ListActivity.this, InfoActivity.class);
startActivity(i);
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
When I click an item and view it, the transition works beautifully. However, using the back button or the Up button(because ListActivity is a parent to InfoActivity) causes both Activities to disappear(not using the sliding transition either) and go back to LoadingActivity.
What is causing this to happen? Is there something seriously wrong with my implementation of the transition?
Aha, I got it! Calling finish() apparently removes the Activity from memory, so I had to remove that line.
The up button in the ActionBar and the back button still didn't do the sliding transition, but that was easily fixed by overriding backPressed() and onOptionsItemSelected() and using overridePendingTransition().
Final code:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//store the id of the item that was clicked
PlacesHelper.tappedId = position;
Intent i = new Intent(ListActivity.this, InfoActivity.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
I hope this helps someone!
Related
I want to go from one fragment to another fragment via click on a widgets.
I used this code in the main activity and used onclick in wedget property:
public void goOther(View v) {
Intent go_To_FragmentTwo = new Intent(this, FragmentTwo.class);
startActivity(go_to_FragmentTwo);
}
please with sample code. and please introduce some articles.
You would need to do the following:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String ID = ((TextView) view.findViewById(R.id.pid)).getText().toString();
Intent intent = new Intent(view.getContext(), EventDetails.class);
intent.putExtra(pid, ID);
view.getContext().startActivity(intent);
}
The code below is actually also passing the id of the selected item from one fragment to another, to then display the details according to the value in the ID.
Hope this helps :)
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!
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.
I'd like to show another List View 'B' after clicking a item of List View 'A'. I use onListItemClick event in Android 1.6 project.
public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(this,
"You have selected " + lv_arr[position],
Toast.LENGTH_SHORT).show();
}
how to code it?
If you want to stay on the same Activity and layout you could use a ViewSwitcher, which is designed for flipping between two views.
However I would strongly suggest that the click triggers a new local Activity via an Intent. This will have a new Layout containing your second ListView. This is because users will expect that having clicked and had the display significantly change, that pressing the back button will take them back to the original location in the app. As a general rule, any user action that changes the conceptual location in the application should be accompanied by an Activity change.
I could see the List View by adding
<activity android:name=".WhiteListView"/>
in AndroidMainfest.xml.
How about try ExpandableListView. When you click on the groupview it expands to show childviews. It has a nice BaseExpandableListAdapter.
For example I call a new activity using Intents, with packed values added this way...
#Override
public void onListItemClick( ListView parent, View v, int position, long id) {
Intent lancon = new Intent(this, viewContact.class);
//lancon.putExtra("id", id);
//or
c.moveToPosition(position);
id = c.getInt(0);
c.close();
lancon.putExtra("id", id);
this.startActivity(lancon);
finish();
}
Then in the other class onCreate method I call:
this._id = this.getIntent().getLongExtra("id", 0);