Get all ListView items? - android

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...

Related

Up navigation with sliding transition in Android

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!

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 to create clickable listview in Android using Sqlite3?

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);
}
});

How to show another List View 'B' after clicking List View 'A'

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);

Categories

Resources