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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:39:23+00:00 2026-06-01T06:39:23+00:00

I am trying to modify the sample code below. It currently populates a View

  • 0

I am trying to modify the sample code below. It currently populates a View that contains an imageview and a textview. I have added an additional textview to my XML layout and am trying to figure out how to replace the simple array with a hash map or even a multidimensional array to populate not just the imageview and the first textview but also the second one.

I would appreciate sample code that shows the entire process. Thanks!

public class DynamicDemo extends ListActivity { 
  TextView selection; 
  private static final String[] items={"lorem", "ipsum", "dolor", 
          "sit", "amet"}
  @Override 
  public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    setListAdapter(new IconicAdapter()); 
    selection=(TextView)findViewById(R.id.selection); 
  } 

  public void onListItemClick(ListView parent, View v, 
                             int position, long id) { 
   selection.setText(items[position]); 
  } 

  class IconicAdapter extends ArrayAdapter<String> { 
    IconicAdapter() { 
      super(DynamicDemo.this, R.layout.row, R.id.label, items); 
    } 

    public View getView(int position, View convertView, 
                       ViewGroup parent) { 
      View row=super.getView(position, convertView, parent); 
      ImageView icon=(ImageView)row.findViewById(R.id.icon); 

      if (items[position].length()>4) { 
        icon.setImageResource(R.drawable.delete); 
      } 
      else { 
        icon.setImageResource(R.drawable.ok); 
      } 

      return(row); 
    } 
  }
} 
  • 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-01T06:39:25+00:00Added an answer on June 1, 2026 at 6:39 am

    The easiest thing to do is use an ArrayAdapter<MyDataObject> where

     public class MyDataObject {
          public String string1;
          public String string2;
          // any other useful attributes
    
     }
    

    And then you would change items to a MyDataObject[] items stored in your class, and instead of doing super.getView(index) you’d do items[index] (which would yield a MyDataObject) and use that data instead.

    Also, importantly: you should use the convertView. And possibly the ViewHolder pattern.

    Edit: At OP’s request, a little more elaboration. Note that this uses the convertView pattern but not the ViewHolder pattern (you should be able to adopt that fairly easily).

    In your Adapter, you’d change getView() as follows:

    public View getView(int position, View convertView, 
                       ViewGroup parent) { 
    
      ViewGroup row;
      if(convertView == null){
          // create your view here.
          row = getLayoutInflater().inflate(R.layout.row);   
      } else {
          row = convertView;
      }
    
      // note:  when you implement ViewHolder, the ViewHolder will
      // hold this reference so that you don't need to look it up every time.
      ImageView icon=(ImageView)row.findViewById(R.id.icon); 
    
      // here you're employing the "items" array that you were using 
      // before, except now it contains MyDataObjects.  pick out the 
      // string (or other data you want to check) from the resulting MyDataObject, 
      // and see if it's longer than 4 characters.
    
      MyDataObject objectAtThisPosition = items[position];
    
      if (objectAtThisPosition.string1.length()>4) { 
        icon.setImageResource(R.drawable.delete); 
      } 
      else { 
        icon.setImageResource(R.drawable.ok); 
      } 
    
      // Do whatever else you want to with objectAtThisPosition.
    
      return(row); 
    } 
    

    That’s it for the easy way, and quite similar to what you have.

    Some more detail; if you don’t care, skip it. 🙂

    I know that Adapters can seem magical, so in the interest of showing how ListView adapters work, here’s an example using a List instead of an Array, so we can remove any magic that ArrayAdapter does with the array behind the scenes. I use a List because they can be more versatile for whatever you’re trying to accomplish (ArrayList or LinkedList or what-have-you).

    To use a List you’d have the following in your Activity:

     private List<MyDataObject> myList = new ArrayList<MyDataObject>();
    

    And instead of items[position] you’d use

     MyDataObject objectAtThisPosition = myList.get(position);
    

    If you want to change your data set dynamically, you should probably use this approach (keeping myList at the Activity level) instead of using an Array and an ArrayAdapter. That would mean you’d need to change from extending ArrayAdapter<String> to just extending BaseAdapter<MyDataObject> (most of the methods in BaseAdapter are trivial to implement) since our data size, for example, would be determined by our list, and not the ArrayAdapter‘s array.

    I know that’s kind of a fire hose, but let me know if you have any questions!

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

Sidebar

Related Questions

I've been trying to modify the AuditTrail code so that it does not copy
I'm trying to modify the sample code for loading 3d model included in ASSIMP's
I am trying to modify the NotePad sample code from the Android developer resources.
So I have a sample application source I am trying to modify for my
Hi I'm trying to modify a web page so that it loads faster. Since
I am trying to modify the controls of a Panel, have it update, then
I'm trying to modify the code from this script . Basically I'm trying to
I'm trying to modify the text of a Label component that I already placed
i've been trying to modify the program so that it could accept more than
I have implemented the regula falsi method. I am trying to modify it so

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.