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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:37:11+00:00 2026-05-25T09:37:11+00:00

I’m pulling my hair out trying to figure this out. From all the tutorials

  • 0

I’m pulling my hair out trying to figure this out. From all the tutorials I’ve read, the way I have this setup should work. I have a ListActivity that is using a custom adapter to display some data. I’d like to display a “no items found” message if the adapter is empty.

This is my layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 style="@style/BaseStyle"
 android:orientation="vertical"
 android:padding="5dip"
 >
      <ListView android:id="@android:id/list"
      style="@style/BaseStyle"
      />

      <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@android:id/empty"
      style="@style/BaseStyle.Title"
      android:text="No Items Found"
      />

      <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/text"
      style="@style/BaseStyle.Title"
      android:layout_alignParentTop="true"
      />
 </RelativeLayout>

This is my code:

 public class Main extends ListActivity {
 private CustomAdapter adapter;
 private String[] items = {};

 @Override     
 public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);

      adapter = new CustomAdapter();

      PopulateItems();

      for (String item : items)
           adapter.addItem(item);

      this.setListAdapter(adapter);
      adapter.notifyDataSetChanged();
 }

 private class CustomAdapter extends BaseAdapter {
 private String[] mData;
 private LayoutInflater mInflater;

 public CustomAdapter() {
      mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 }

 public void addItem(String item) {
      mData.add(item);
 }

 @Override
 public int getCount() {
      return mData.size();
 }

 @Override
 public Object getItem(int position) {
      return mData.get(position);
 }

 @Override
 public long getItemId(int position) {
      return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent)
 {
      ViewHolder holder;
      String item = (String)this.getItem(position);

      if (convertView == null)
      {         
           holder = new ViewHolder();

           convertView = mInflater.inflate(R.layout.main, parent, false);
           holder.text = (TextView)convertView.findViewById(R.id.text);
           convertView.setTag(holder);
      } else {
           holder = (ViewHolder)convertView.getTag();
      }
      TextView tvText = holder.text;
      tvText.setText(item);
 }

 static class ViewHolder {
      TextView text;
 }

What happens is the “No Items Found” text displays fine, when the “items” string array has no data. There is a process in the PopulateItem() method that will populate the array with data, and if there is data, the “No Items Found” text can still be seen on each row of the listing, “underneath” the data. So the empty text is basically being overlaid by the data.

  • 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-25T09:37:12+00:00Added an answer on May 25, 2026 at 9:37 am

    I’m not sure if you made any typos in your question for your code. It looks like you strip out parts of your code before posting it here.

    getView() is meant for you to craft a View for a particular row in your ListView. I’m guessing you actually inflated your main.xml, which is your RelativeLayout containing the ListView, EmptyView and another TextView.

    If your ListView is only dislaying strings, just instantiate a TextView and return it in getView

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
      String item = (String)this.getItem(position);
    
      if (convertView == null)
      {         
           convertView = new TextView(context);
      } 
      convertView.setText(item);
      return convertView
    }
    

    Pass in the Context through the constructor of your custom adapter

    Edit: Showing multiple textviews in a single row

    If I’m going to be strict about it, the problem is not in the line convertView = mInflater.inflate(). The error is in the file inflated. It is usually easier to inflate another xml file if you have a more complex view for each row in your listview. The earlier mistake is somehwhat seemingly recursive inflating of main.xml

    To handle multiple textview, do sth like this:

    Create another xml file to represent each row in your listview, list_item.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      .....
     >
      <TextView android:id="@+id/list_text_1"
        ....
      />
    
      <TextView android:id="@+id/list_text_2"
        ....
      />
    
      <TextView android:id="@+id/list_text_3"
        ....
      />
     </LinearLayout>
    

    In your getView implementation in your adapter,

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
      String item = (String)this.getItem(position);
    
      if (convertView == null)
      {         
         convertView = mInflater.inflate(R.layout.list_item.xml, parent, false);
         //link list_text_1, list_text_2, list_text_3 as you need to the individual   
         // textviews
      } 
      else {
        // TODO linking up the textviews when convertView is not null
      }
      return convertView
    }
    

    This is probably a good time to bring in the viewholder pattern considering you need to inflate a xml file. Page 11/19 of the pdf file linked in this answer gives a pretty good explanation about the ViewHolder pattern.

    Lastly, do read through more examples of ListView, and observe when do they use the inflater in getView and what they actually inflate.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a text area in my form which accepts all possible characters from
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.