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 made..my next step is to make each item call on its own activity.

Any Assistance  will be appreciated. 

thank you

Post a Reply

Replies

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
.findViewById(R.id.playerId);

String playerIdValue = playerId.getText()
.toString();

// 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);
startActivity(theIndent);
}
});
ListAdapter adapter = new PlayerAdapter(getSherlockActivity(),playerList, R.layout.player_checkable_entry);
playersListView.setAdapter(adapter);
}
else
playersListView.setAdapter(null);
}

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_layoutId=layoutId;
        m_context= context;
        m_playerArrayList=playerArrayList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
    Player player=m_playerArrayList.get(position);
        LayoutInflater inflater = (LayoutInflater) m_context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(m_layoutId, parent, false);
        if(m_layoutId==R.layout.player_checkable_entry)
        {
        TextView playerId = (TextView) view.findViewById(R.id.playerId);
        TextView firstName = (TextView) view.findViewById(R.id.firstName);
        TextView lastName=(TextView) view.findViewById(R.id.lastName);
        TextView chessRating=(TextView)view.findViewById(R.id.chessRating);
        CheckBox playerCheckBox=(CheckBox)view.findViewById(R.id.playerCheckBox);
        playerId.setText(player.getPlayerId());
        firstName.setText(player.getFirstName());
        lastName.setText(player.getLastName());
        chessRating.setText(player.getChessRating());
        playerCheckBox.setChecked(false);
        }
        
        if(m_layoutId==R.layout.player_entry)
        {
        TextView playerId = (TextView) view.findViewById(R.id.playerId);
        TextView firstName = (TextView) view.findViewById(R.id.firstName);
        TextView lastName=(TextView) view.findViewById(R.id.lastName);
        TextView chessRating=(TextView)view.findViewById(R.id.chessRating);
        playerId.setText(player.getPlayerId());
        firstName.setText(player.getFirstName());
        lastName.setText(player.getLastName());
        chessRating.setText(player.getChessRating());
        }
        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.support.v7.app.ActionBarActivity;
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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cities);

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


        String[] Cities = {
                "New York",
                "Miami",
                "Atlanta",
                "Orlando",
                "Calgary",
                "Toronto",
                "Dallas",
                "Austin",
                "San Diego",
                "Las Vegas"};
        ListAdapter cityAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Cities);
        ListView cityListView = (ListView) findViewById(R.id.citiesListView);
        cityListView.setAdapter(cityAdapter);

        cityListView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    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. 

@Mario

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
@Mario

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

107,058 followers
About

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

Links
Moderators
Bucky Roberts Administrator