[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListViews on Android

Posted on 2011-02-22
4
Medium Priority
?
834 Views
Last Modified: 2012-05-11
Hey,

        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!
0
Comment
Question by:errang
4 Comments
 
LVL 47

Expert Comment

by:for_yan
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
0
 

Author Comment

by:errang
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.app.Activity;
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) {
		super.onCreate(savedBundleInstance);
		setContentView(R.layout.vehicle);
		ArrayList<String> strArr = new ArrayList<String>();

	   	strArr.add("one");
		strArr.add("two");
		strArr.add("three");
		strArr.add("four");

		TextView tv = new TextView(this);
		tv.setText("TestView");
		ListView lv = (ListView)findViewById(R.id.listView);
		lv.addHeaderView(tv);
		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.
0
 
LVL 5

Accepted Solution

by:
aciuica earned 1000 total points
ID: 34958855
You can search for startActivity
For example here is a sample:
http://www.androidcompetencycenter.com/2009/03/tutorial-how-to-start-a-new-activity/#
and first comment show also a posibility to pass parameters to sarted activity.
0
 
LVL 17

Assisted Solution

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

lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  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);
    Vehicle.this.startActivity(selectedIntent);    
  }
});
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There is a lot to be said for protecting yourself and your accounts with 2 factor authentication.  I found to my own chagrin, that there is a big downside as well.
Ransomware, the malware that locks down its victim’s files until they pay up, has always been a frustrating issue to deal with. However, a recent mobile ransomware will make the issue a little more personal… by sharing the victim’s mobile browsing h…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

868 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