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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:44:48+00:00 2026-06-15T08:44:48+00:00

As my post states, I can’t seem to get my ArrayAdapter to update the

  • 0

As my post states, I can’t seem to get my ArrayAdapter to update the whole list despite the call "adapter.notifyDataSetChanged();" Only the last element in the list is updated.

Can someone maby see what I’m doing wrong below??

Here is my custom adapter

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class WeatherAdapter extends ArrayAdapter<Weather>{

    Context context; 
    int layoutResourceId;    
    Weather data[] = null;
    private WeatherHolder holder;
    private Weather weather;


    public WeatherAdapter(Context context, int layoutResourceId, Weather[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new WeatherHolder();
            holder.imgIcon = (TextView)row.findViewById(R.id.imgen);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (WeatherHolder)row.getTag();
        }

        weather = data[position];
        holder.txtTitle.setText(weather.getName());
        //holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
        return row;
    }

    public void update(String buttonPressed){
        if(buttonPressed == "Köp Kurs"){
                            holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));//This updates only the last element in list but I want to update every element in the list



        }
        else if(buttonPressed == "Antal"){              holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));//This updates only the last element in list but I want to update every element in the list


        }
    }

    static class WeatherHolder
    {
        TextView imgIcon;
        TextView txtTitle;
    }
}

And here is my main class, when I call “Update()” method only the last element in the list is updated but not the others. How can I update the whole list instead of just the last element in the list?

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listView1;
    private Button goButton;
    private String[] listheader = {"Köp Kurs","Antal"};
    private WeatherAdapter adapter;
    private int totalElemInlist = listheader.length;
    private int currentelemInList=0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Weather weather_data[] = new Weather[]
        {
            new Weather("ABB", 56.0, 300),
            new Weather("Volvo", 89.0,500),
            new Weather("Astra Zeneca", 98.55, 50)
        };

        adapter = new WeatherAdapter(this, 
                R.layout.listview_item_row, weather_data);


        listView1 = (ListView)findViewById(R.id.listView1);

        View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
        listView1.addHeaderView(header);
        goButton = (Button) findViewById(R.id.testButton);
        goButton.setText(listheader[currentelemInList]);

        listView1.setAdapter(adapter);


        goButton.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                String buttonPressed = (String) ((Button) view).getText();

                goThroughList(buttonPressed);
                System.out.println("Button Clicked" + buttonPressed);
            }
          });
    }

    private void goThroughList(String buttonPressed){
        currentelemInList++;
        if(currentelemInList>=totalElemInlist){
            currentelemInList=0;
        }
        goButton.setText(listheader[currentelemInList]);

        if(buttonPressed == "Köp Kurs"){
            System.out.println("Köp kurs");
            adapter.update(buttonPressed);
            adapter.notifyDataSetChanged();//This only updates the last element in list
        }
        else if(buttonPressed == "Antal"){
            System.out.println("Antal");
            adapter.update(buttonPressed);
            adapter.notifyDataSetChanged();//This only updates the last element in list
        }
        System.out.println(currentelemInList);
    }
}
  • 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-15T08:44:49+00:00Added an answer on June 15, 2026 at 8:44 am

    How can I update the whole list instead of just the last element in the list?

    You must make your changes happen in getView(). getView() redraws every row as they appear whenever the user scrolls the ListView or notifyDataSetChanged() is called. So the changes must happen in there otherwise they will be erased.

    First create a new field variable in your adapter:

    private String currentImage;
    

    Second change update() to control the contents on imgIcon:

    public void update(String buttonPressed){
        currentImage = buttonPressed;
        notifyDataSetChanged();
    }
    

    Last change getView() to display the current images for each row:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
    
        weather = data[position];
        holder.txtTitle.setText(weather.getName());
    
        if(currentImage.equals("Köp Kurs")){
            holder.imgIcon.setText(Double.toString(weather.getBuyingRate()));
        }
        else if(currentImage.equals("Antal")){
            holder.imgIcon.setText(Double.toString(weather.getNrOfSharesInPortfolio()));
        }
        return row;
    }
    

    (You can also simplify goThroughList(), since the if-else contains the exact same code.)

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

Sidebar

Related Questions

The PHP documentation states that php://input can only be read once. In my application
This post from F# News states that F# can inline a function passed as
I came across this post here (link below) and it states that you can
I am looking for a list of countries/states/cities that can be used with Facebook
By @user.posts , I can see there is a post with :unfinished status. But
An informative-sounding blog post from 2006 states these facts about using native prepared statements
Couldn't comment on this post, which states the problem I'm experiencing: jQuery remove() on
Okay so here is the deal. As the question states, I'm trying to POST
For some reason when I call a namespace within this class, I get an
I have a page that uses jQuery's $.post() method to get some html and

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.