List View

+1 Mario Hutch · May 25, 2015
Good Day to all, 

I am trying to create an activity where I have a list of items in a listview. In that list view I would like each item to open to its own activity/window... I already have the list next step is to make each item call on its own activity.

Any Assistance  will be appreciated. 

thank you

Post a Reply


Oldest  Newest  Rating
0 Dol Lod · May 26, 2015
I recommend a tutorial series by Derek Banas. He posts source code on his website.

To learn how to use custom ListAdapter, use Google. 

However, I can provide a general summary of what to do:

1) Standardize the data format of each item i.e. use built in structures for simplicity like hashmaps. Custom classes are harder, but can still be done. Then you will need an arraylist of those elements which is passed in to the listview along with the adapter.

I am also posting a bit of the code which is relevant to what you want. Code tags are not working in any capacity so I am just posting as is.

Relevant information: playerList is an arraylist of the player class. Within the setOnItemClickListener, I am using text fields I declared in a custom layout I created for each entry in the ListView. That view contains textfields like playerId, name and receive the information from each object in the player class within the ArrayList playerList.

Like I said, custom adapters are needed for custom classes, but it also means you can have improved performance. However, things like HashMaps are simpler to use. 

public void updateListView()
playerList = dbTools.getAllPlayers();
if (playerList.size() != 0)
playersListView.setOnItemClickListener(new OnItemClickListener()

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

playerId = (TextView) view

String playerIdValue = playerId.getText()

// Signals an intention to do something
// getApplication() returns the application that owns
// this activity

Intent theIndent = new Intent(getSherlockActivity().getApplication(),ViewPlayer.class);
theIndent.putExtra("playerId", playerIdValue);
ListAdapter adapter = new PlayerAdapter(getSherlockActivity(),playerList, R.layout.player_checkable_entry);

Here is the portion of code relating to the custom adapter. 

public class PlayerAdapter extends ArrayAdapter<Player>
private final Context m_context;
    private final ArrayList<Player> m_playerArrayList;
    private int m_layoutId;

    public PlayerAdapter(Context context, ArrayList<Player> playerArrayList, int layoutId) {

        super(context,layoutId, playerArrayList);
        m_context= context;

    public View getView(int position, View convertView, ViewGroup parent)
    Player player=m_playerArrayList.get(position);
        LayoutInflater inflater = (LayoutInflater) m_context
        View view = inflater.inflate(m_layoutId, parent, false);
        TextView playerId = (TextView) view.findViewById(;
        TextView firstName = (TextView) view.findViewById(;
        TextView lastName=(TextView) view.findViewById(;
        TextView chessRating=(TextView)view.findViewById(;
        CheckBox playerCheckBox=(CheckBox)view.findViewById(;
        TextView playerId = (TextView) view.findViewById(;
        TextView firstName = (TextView) view.findViewById(;
        TextView lastName=(TextView) view.findViewById(;
        TextView chessRating=(TextView)view.findViewById(;
        return view;
0 Mario Hutch · June 11, 2015
Below is my code, it does not seem to run. and I was also wondering which is best to use the "If" or "case"  statements in that case. I'm not to sure where errors are.

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android. view.View;
import android. widget. AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Cities extends Activity {

    ListView cityListView;

    protected void onCreate(Bundle savedInstanceState) {

        //get list view from xml
        cityListView = (ListView) findViewById(;

        String[] Cities = {
                "New York",
                "San Diego",
                "Las Vegas"};
        ListAdapter cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Cities);
        ListView cityListView = (ListView) findViewById(;

                new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String cities = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(Cities.this, cities, Toast.LENGTH_LONG).show();

                        if (position == 1) {
                            //code specific to first list item
                            Intent myIntent = new Intent(view.getContext(), NewYork.class);
                            startActivityForResult(myIntent, 0);



0 Dol Lod · June 11, 2015
@Kayler 90% of the time I copy code and paste it into a code block, it shows up incorrectly. 


ListView cityListView is declared twice, both within the activity and the method. You should only be using the one declaredw ithin the activity. 

I also think maybe the position would be 0, not 1 for the first item. 
0 Mario Hutch · June 11, 2015
@Arjun Petel... I got the code running, I did change the position to 0...but still it would not call the other activity. I clicked on the first item and it gave me the Unfortunately app has stop error.
0 Dol Lod · June 12, 2015

Try replacing the intent you are using with the following:
Intent myIntent = new Intent(view.getContext(), NewYork.class);

Make sure your manifest has the NewYork.class activity declared.

If it continues to not work, try changing startActivityForResult(myIntent, 0); to startActivity(myIntent);

I have no idea how startActivityForResult works so I assume that you know what you are doing. However, the intent should now work correctly.

Since this is an Android application, you are the only one who can test the code changes. I would start trying to debug your application using LogCat and see what is happening if this continues to be a problem.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator