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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:00:42+00:00 2026-05-25T02:00:42+00:00

I’m trying to make a custom listview that will hold views of custom objects,

  • 0

I’m trying to make a custom listview that will hold views of custom objects, in this case, the custom objects will be instances of class Data

Here’s the Java code:

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListsActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Data[] array = new Data[3];
        Data p1 = new Data("Person 1", 20);
        Data p2 = new Data("Person 2", 33);
        Data p3 = new Data("Person 3", 22);
        array[0] = p1;
        array[1] = p2;
        array[2] = p3;
        setListAdapter(new MyAdapter(this, R.layout.row, array));
    }

    private class MyAdapter extends ArrayAdapter<Data>{

        private Data[] data;
        public MyAdapter(Context context, int textViewResourceId,
                Data[] objects) {
            super(context, textViewResourceId, objects);
            data = objects;
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        if(convertView == null){
            convertView = inflater.inflate(R.layout.row, parent);
        }
        TextView text = (TextView) findViewById(R.id.textView1);
        if(data != null){
            text.setText(data[position].getName());
        }
        return convertView;
    }

}  

}

class Data{
    private String name;
    private int age;

public Data(String name, int age) {
    this.name = name;
    this.setAge(age);
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
    }

}

and the xml files
row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal" android:weightSum="1">

    <ImageView android:src="@drawable/icon" android:id="@+id/imageView1"
        android:layout_height="wrap_content" android:layout_width="wrap_content" />

    <TextView android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/textView1" android:layout_height="wrap_content"
        android:text="TextView" android:paddingTop="25dp"
        android:layout_width="match_parent" />

</LinearLayout>

and main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:layout_height="wrap_content" android:id="@android:id/list"
        android:layout_width="match_parent" />

</LinearLayout>

But it throws an UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

  • 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-25T02:00:42+00:00Added an answer on May 25, 2026 at 2:00 am

    I solved it

    I created my own Adapter by extending BaseAdapter, and then calling the findViewByID method of the object convertView

    The code for my adapter follows, in case anyone else runs into this problem

    private class MyAdapter extends BaseAdapter{
    
            private List<Data> data;
            private LayoutInflater inflater;
            private TextView text;
    
            public MyAdapter(Data[] contents){
                data = new ArrayList<Data>();
                for(Data d : contents){
                    data.add(d);
                }
                inflater = getLayoutInflater();
    
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if(convertView == null){
                    convertView = inflater.inflate(R.layout.row, null);
                }
                if(data != null){
                    text = (TextView) convertView.findViewById(R.id.textView1);
                    text.setText(data.get(position).getName());
                }
                return convertView;
            }
    
    
            @Override
            public int getCount() {
                return data.size();
            }
    
    
            @Override
            public Object getItem(int arg0) {
                return data.get(arg0);
            }
    
    
            @Override
            public long getItemId(int position) {
                return data.get(position).hashCode();
            }
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text

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.