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 3427532
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:50:53+00:00 2026-05-18T06:50:53+00:00

I need to display a list of categories from a ListActivity. My data source

  • 0

I need to display a list of categories from a ListActivity. My data source is a ArrayList of type Category which is implementing the android Parcelable interface. stucture is given at the bottom. it has an id and a title

I need title to be displayed as list text and on clicking the list item, i need to get the id for further processing

It is possible to create a new array of ‘title’s by iterating the category ArrayList and use
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.label, names));

But bu using this method i will get only the title of the category upon click and not the id.

Is it possible to directly use the ArrayList of objects as data source?

public class CategoryList extends ListActivity  {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Intent t = new Intent(this, ParsingWebXML.class);
        startActivityForResult(t,1);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 1 && resultCode == RESULT_CANCELED){
            finish();
        } else {
            createCategoryList(data);
        }
    }

    private void createCategoryList(Intent data) {
        ArrayList<Category> categories = null;
        try {
            categories = data.getParcelableArrayListExtra("categoryList");
            Iterator<Category> itr = categories.iterator();

            String[] names = new String[categories.size()];
            int i=0;
            while (itr.hasNext()) {
                Category c = itr.next();
                names[i++] = c.getTitle();
            }
            this.setListAdapter(new ArrayAdapter<Category>(this, R.layout.rowlayout, R.id.label, categories));
            // String[] names = new String[] { "Sports", "Mediacal", "Computers" };
            // this.setListAdapter(new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.label, names));
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();

        if(keyword=="Categories"){
            Intent cat = new Intent(this, CategoryActivity.class);
            startActivity(cat);
            //Toast.makeText(this, keyword, Toast.LENGTH_LONG).show();
        } else {
            Intent cat = new Intent(this, CategoryActivity.class);
            startActivity(cat);
        }

    }

}

Category.java

public class Category implements Parcelable {

    private int id;
    private String title;

    public Category() {

    }

    public Category(Parcel source) {
        id = source.readInt();
        title = source.readString();
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getId() {
        return this.id;
    }

    public String getTitle() {
        return this.title;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
          dest.writeInt(id);
          dest.writeString(title);
    }

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

        @Override
        public Category createFromParcel(Parcel source) {
            return new Category(source);
        }

        @Override
        public Category[] newArray(int size) {
            return new Category[size];
            // TODO Auto-generated method stub
        }

    };

}
  • 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-05-18T06:50:54+00:00Added an answer on May 18, 2026 at 6:50 am

    Here are two good examples of how to make your custom implementation of the ArrayAdapter class:

    • Android Series: Custom ListView items and adapters
    • Custom ArrayAdapter, with different icons
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to display the list of names which I am getting from the
I need to display a list of dates, which I have in a table
I need to display a single list, ordered by date which contains different types
I need to display these list elements starting from top, I don't want them
I need to display list of news items from a database on a web
I need to display the log entries from a database. Of course the log
i need to display the list one by one in the same line. It
I need to display a list of items in a page, and this page
I need to display a list of comments within a view. I have created
I need to display a list on a ListBox component in VB .NET. //

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.