ListViews on Android

Posted on 2011-02-22
Last Modified: 2012-05-11

        I was trying to use ListViews to show a menu, and when one of the options was clicked, jump to a different activity, and contain some variables so the activity can carry out the operation.

I did get the ListView to work, because it was just placing an ArrayList inside the adapter, and it would work fine... but I'm having problems making it switch to the new activity properly...

I tried searching for tutorials on this, but I don't exactly know what I'm looking for...

Appreciate any help on this!
Question by:errang
LVL 47

Expert Comment

ID: 34957151
Perhaps you'd post some part of your code and be a little bit more specific
on what you observe with regards to the undesired behavior

Author Comment

ID: 34957522
I can't post the entire code... but I've rewritten it to a "hello world" level problem, so I can understand things more clearly.

import java.util.ArrayList;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Vehicle extends Activity {
	public void onCreate(Bundle savedBundleInstance) {
		ArrayList<String> strArr = new ArrayList<String>();


		TextView tv = new TextView(this);
		ListView lv = (ListView)findViewById(;
		lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strArr));

Open in new window

I figured out how to figure out what choice was clicked... but I have no idea how to launch a different activity that would do what its supposed to do.

Accepted Solution

aciuica earned 250 total points
ID: 34958855
You can search for startActivity
For example here is a sample:
and first comment show also a posibility to pass parameters to sarted activity.
LVL 17

Assisted Solution

by:Dejan Pažin
Dejan Pažin earned 250 total points
ID: 34960533
Add this to your code:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    Object clickedItem = lv.getItemAtPosition(position);
    // Depending on the clicked item value, open new activity:
    Intent selectedIntent = new Intent(Vehicle.this, SomeOtherActivity.class);

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now