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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:41:18+00:00 2026-06-04T05:41:18+00:00

String[] temp = new String[adapter.getCount()]; for(int i = 0; i < adapter.getCount(); i++) temp[i]

  • 0
String[] temp = new String[adapter.getCount()];

        for(int i = 0; i < adapter.getCount(); i++)
            temp[i] = adapter.getItem(i).toString();


        List<String> list = Arrays.asList(temp);

        Collections.sort(list);

        adapter.clear();

        comment = new Comment();

        for(int i = 0; i < temp.length; i++)
        {
            comment.setComment(temp[i]);
            System.out.println("comment is: " + comment.getComment());
            adapter.insert(comment, i);
            System.out.println("adapter is: " + adapter.getItem(i));

        }

        for(int i = 0; i < adapter.getCount(); i++)
            System.out.println(adapter.getItem(i));

The code above performs the sorting of the ArrayAdapter which is typed ; a helper class as I am using SQLiteHelper and SQL database.

Ok so I verify, after clearing all data within the ArrayAdapter, that the data is added within a lexicographic sorted order.

However, by the time I reach the final for loop to verify this, the ArrayAdapter has replicated the last item in the list at every index. This is weird and makes no sense to me. Of course this is also reflected on the screen.

Can you provide assistance to understand what is going please?

  • 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-04T05:41:21+00:00Added an answer on June 4, 2026 at 5:41 am

    You are using the same instance of ‘Comment’ throughout the ArrayAdapter. Hence, all positions of the ArrayAdapter have the exact same ‘comment’ object reference. This single instance has been set to the final string from the original list and so all ListView items will look the same. The solution is to move the instantiation of ‘comment’ into the for loop to create a unique ‘comment’ instance for each adapter position. I’ve also slightly optimized your code.

    // -- Count used repeatedly, particularly in for loop - execute once here.
    int orgCount = adapter.getCount();
    
    String[] temp = new String[orgCount];
    
    for(int i = 0; i < orgCount; i++)
        temp[i] = adapter.getItem(i).toString();
    
    
    List<String> list = Arrays.asList(temp);
    
    Collections.sort(list);
    
    // -- Prevent ListView refresh until all modifications are completed.
    adapter.setNotifyOnChange(false);
    adapter.clear();
    
    
    for(int i = 0; i < temp.length; i++)
    {
        // -- Instantiation moved here - every adapter position needs a unique instance.
        comment = new Comment();
        comment.setComment(temp[i]);
        System.out.println("comment is: " + comment.getComment());
        // -- Changed from insert to add.
        adapter.add(comment);
        System.out.println("adapter is: " + adapter.getItem(i));
    }
    
    for(int i = 0; i < adapter.getCount(); i++)
        System.out.println(adapter.getItem(i));
    
    // -- Auto notification is disabled - must be done manually.
    adapter.notifyDataSetChanged(true);
    // -- All modifications completed - change notfication setting if desired.
    adapter.setNotifyOnChange(true);
    

    EDIT
    Also, since you’re inserting/adding one at a time, you might want to delay notifyDataSetChanged from executing until after all modifications are completed. This will prevent the ListView from refreshing on every modification. I’ve included it in the code above.

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

Sidebar

Related Questions

For example I have: public static List<int> actorList = new List<int>(); public static List<string>
I have code like this: string uriString = @C:\Temp\test.html; Uri uri = new Uri(uriString);
SOLUTION Thanks to 1111... vector<std::string> split_at_line(string str, int lines) { vector<std::string> nine_ln_strs; string temp;
string temp is equal to ZERO:\t.WORD\t1 from my debugger. (the first line of my
string popUpHTML=; var xx = from Temp in TemplateList where ( Temp.TitleID == titleID
string fileName = test.zip; string path = c:\\temp\\; string fullPath = path + fileName;
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
I'm using the following code to populate listview Oncreate method: SimpleAdapter adapter = new
There is a String temp = M0-1 How to convert this String to a
The String will looks like this: String temp = IF (COND_ITION) (ACT_ION); // Only

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.