Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:19:52+00:00 2026-06-15T09:19:52+00:00

I am trying to create a basic List view application. I had it working

  • 0

I am trying to create a basic List view application.
I had it working fine with the default system array adapter. I then tried to modify it to allow me to use a custom adapter. Unfortunately I have run into a problem.

When I run the application I get no runtime errors, rather I just get a black screen where the list should be displayed which makes me think that perhaps it is displaying a list but for some reason the adapter is not finding the data….

Anyways, Below is my List activity:

    package com.example.waitronproto2;

    import java.util.ArrayList;
    import java.util.List;

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    public class AppetizersActivity extends ListActivity{

            //Instance Variables
            //Array to hold menu appetizers - In future this will be pulled from web server
            private List<MenuItem> appetizers;
            private MenuItemArrayAdapter appetizersAdapter;


            @Override
            public void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                //Garbage collection
                System.gc();
                setContentView(R.layout.appetizers_list);

                fillList();

                //Create custom array adapter - links list with source of data, an array.
                appetizersAdapter = new MenuItemArrayAdapter(this, appetizers);


                //Set the newly created adapter as the lists adapter.
                this.setListAdapter(appetizersAdapter); 
            }

            @Override
            protected void onListItemClick(ListView l, View v, int position, long id){
                //Toast for testing
                Toast.makeText(this.getApplicationContext(), l.getItemAtPosition(position) + " Clicked!", Toast.LENGTH_LONG).show();        

                //store clicked item in String variable
                String choice = l.getItemAtPosition(position).toString();

                //add to an array that is being passed from Activity to Activity
            }

            //fill the list with  menu items
            private void fillList(){
                appetizers = new ArrayList<MenuItem>();
                appetizers.add(new MenuItem("Appetizer 1", "Appetizer", 8.95));
                appetizers.add(new MenuItem("Appetizer 2", "Appetizer", 9.95));
                appetizers.add(new MenuItem("Appetizer 3", "Appetizer", 12.95));
                appetizers.add(new MenuItem("Appetizer 4", "Appetizer", 4.95));
                appetizers.add(new MenuItem("Appetizer 5", "Appetizer", 5.95));
                appetizers.add(new MenuItem("Appetizer 6", "Appetizer", 7.95));
                appetizers.add(new MenuItem("Appetizer 7", "Appetizer", 5.95));
                appetizers.add(new MenuItem("Appetizer 8", "Appetizer", 12.95));
                appetizers.add(new MenuItem("Appetizer 9", "Appetizer", 10.95));


            }
    }

and here is my adapter class:

    package com.example.waitronproto2;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

//Default array adapter for menu items
public class MenuItemArrayAdapter extends ArrayAdapter<MenuItem> {

    private List<MenuItem> menuItems;

    //Constructor
    public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems) {
        super(context, R.layout.menuitem_row);
        this.menuItems = menuItems;
    }

    //get views
    public View getView(final int position, View convertView, ViewGroup parent){
        View v = convertView;


        if(v == null){
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.menuitem_row, null);
        }

        //assign values to view
        MenuItem item = this.menuItems.get(position);

        TextView nameView = (TextView) v.findViewById(R.id.item_name);
        TextView priceView = (TextView) v.findViewById(R.id.item_price);


        nameView.setText(item.getName());
        priceView.setText(String.valueOf(item.getPrice()));

        return v;

    }

}

Any ideas where I might be going wrong?

Many thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:19:52+00:00Added an answer on June 15, 2026 at 9:19 am

    By extending ArrayAdapter but using a local list in your custom adapter, you muddled things up a bit. ArrayAdapter doesn’t know the list has any data because its getCount() references an empty list.

    Use this super constructor instead:

    //Constructor
    public MenuItemArrayAdapter(Context context, List<MenuItem> menuItems) {
        super(context, R.layout.menuitem_row, menuItems);
        //  Pass the ArrayAdapter your list   ^^^^^^^^^
    }
    

    And don’t keep menuItems yourself or you code will become overly complicated over time. You’ll need to use getItem(position) where you currently use this.menuItems.get(position).


    Alternatively you can override getCount(), getItem(), and getItemId() to point to your menuItems list. But if you do this, you might as well extend BaseAdapter directly since you have taken away must of the benefits of ArrayAdapter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a list view which as TextView that can display
I'm trying to create a view that contains a list of checkboxes that is
i'm trying to create basic application on Play Frramework(1.2.2) + siena (2.0.2) + crudsiena(2.0.1)
I'm trying to create a basic for loop which creates an array but am
I have a very basic application in which I am trying to create ListFragment
I am trying to add a basic dropdown list to my view with a
I am trying to create a basic navigation bar using an unordered list, but
Im trying to create a basic JSF application but can't seem to get it
I'm trying to create a basic shopping cart, having an issue with the product
I'm trying to create a basic database trigger that conditionally deletes rows from database1.table1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.