Setting new pages linked by an Activity

In the application I am writing, I have a main class which extends the ListActivity - it contains a list of elements. I want these elements to link to a different page (an xml one, or perhaps a View object). However, I realised one cannot use the method setContentView(int) on a ListActivity object.

What's to be done?

Thanks!

Answers


Looks like you are trying to launch a new activity.

You have to override the onListItemClick method of ListActivity.

Here is the code.

// ListView l points to list view whose item user clicked
// View v points to the item in the list view on which the user clicked
// int position is the position index of the item in the list
// long id is the id assigned to the item. This id is assigned using the ListAdapter, CursorAdapter etc.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    // I am using getApplicationContext() as it is more safe then just passing `this`
    Intent i = new Intent(this.getApplicationContext(), ActivityToRun.class);

    this.startActivity(i);
}

NOTE: You have to improve on this skeleton depending upon your needs.


Need Your Help

What are the key differences between “reusability” and “portability”?

coding-style software-engineering software-design convention

I was reading a book on how to write good code. While doing so, I found two words with similar meanings on Wikipedia and other sources. Hence my question:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.