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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:23:38+00:00 2026-06-16T19:23:38+00:00

In the following code I have set up a fragment that shows 2 ListViews.

  • 0

In the following code I have set up a fragment that shows 2 ListViews.

When the main activity call the Fragment methods:

 prendiListaBest

and

 prendiListaWorst

they call notifyDataSetChanged() on the adapters,

therefore the lists should update with the new contents of the ArrayLists.

But this does not happen.

Please any help and suggestion more than welcome

public class WorstBest extends Fragment {
    public ListView lvBest;
public ListView lvWorst;
public MyAdapter myAdap;
public MyAdapter myAdap2;
public ArrayList<Stock> best;
public ArrayList<Stock> worst;
public View v;
   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        v= inflater.inflate(R.layout.listaworstbest, container, false);    
        lvBest= (ListView)v.findViewById(R.id.listbest);
        lvWorst=(ListView)v.findViewById(R.id.listworst);;
        myAdap= new MyAdapter(best, getActivity());
        myAdap2= new MyAdapter(worst, getActivity());
        lvBest.setAdapter(myAdap);
        lvWorst.setAdapter(myAdap2);
        return v;              
    }

    public void prendiListaBest(ArrayList<Stock> al){
        Log.e("", "passo per prendilitsa del fragment worstbest");
        best=al;
        Log.e("", "nel fragment best è" + Integer.toString(best.size()));
        myAdap.notifyDataSetChanged();
    }    

    public void prendiListaWorst(ArrayList<Stock> al){
        Log.e("", "passo per prendilitsa del fragment worstbest");
        worst=al;
        Log.e("", "nel fragment worst è" + Integer.toString(worst.size()));
        myAdap2.notifyDataSetChanged();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
    }
}

This is the code of my custom adapter.
I posted as requested but I apologize because it is really messy:

public class MyAdapter extends BaseAdapter   {
    ArrayList<Stock> l; 
    Activity a;

public MyAdapter(ArrayList<Stock> li, Activity ac) {
        // TODO Auto-generated constructor stub
    l = li;
    a=ac;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row=convertView;

    if (row==null) {
    LayoutInflater inflater=a.getLayoutInflater();
    row=inflater.inflate(R.layout.row, parent, false);
    }   
    TextView tw=(TextView)row.findViewById(R.id.textView1);

    String st=l.get(position).simbolo;
    String st1=st;
    String st2=st;
    String st3=st;
    String st4=st;
    String st5=st;
    String st6=st;
    String st7=st;
    String st8=st;
    st8=st;
        if(st.length()<=7){st1=st.concat("     ");st2=st1;st3=st1;st4=st1;st5=st1;st6=st1;st7=st1;st8=st1;}
    if(st1.length()<=7){st2=st1.concat("     ");st3=st2;st4=st2;st5=st2;st6=st2;st7=st2;st8=st2;}
    if(st2.length()<=7){st3=st2.concat(" ");st4=st3;st5=st3;st6=st3;st7=st3;st8=st3;}
    if(st3.length()<=7){st4=st3.concat(" ");st5=st4;st6=st4;st7=st4;st8=st4;}
    if(st4.length()<=7){st5=st4.concat(" ");st6=st5;st7=st5;st8=st5;}
    if(st5.length()<=7){st6=st5.concat(" ");st7=st6;st8=st6;}
    if(st6.length()<=7){st7=st6.concat(" ");st8=st7;}
    if(st7.length()<=7){st8=st7.concat(" ");}

    tw.setText(st8);
    String allora=(String) tw.getText();    
    TextView tw2=(TextView)row.findViewById(R.id.nome);
    ///aggiungiamo numero minimo caratteri
    tw2.setText((l.get(position).nome));
    ///
    TextView tw3=(TextView)row.findViewById(R.id.prezzo);
    tw3.setText((l.get(position).prezzo));

    TextView tw4=(TextView)row.findViewById(R.id.cambiamento);
    tw4.setText((l.get(position).cambiamento).concat(" %"));

    try {
    double value = Double.parseDouble(l.get(position).cambiamento);
    if(value<0)tw4.setTextColor(Color.RED);
    if(value>0)tw4.setTextColor(Color.GREEN);
    TextView tw5=(TextView)row.findViewById(R.id.freccia);
    if(value<0)tw5.setText("▼");
    if(value>0)tw5.setText("▲");
    if(value<0)tw5.setTextColor(Color.RED);
    if(value>0)tw5.setTextColor(Color.GREEN);
        } catch (Exception exc) {}

    return row; 

 }

 public int getCount() {
    // TODO Auto-generated method stub
    return l .size();

}

 public Object getItem(int position) {
    // TODO Auto-generated method stub
    return l .get(position);

 }

 public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
 }
 }

EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT *EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT EDIT *

Thank you very much, the answer makes perfectly sense.**

But I do not understand why the same Adapter works fine with the following fragment; I explain:
the following “listafragment” fragment has a similar logic, it uses the

 prendiLista

method to retrieve the latest version of the

 ListArray lt

exactly the same way it is done in the previous “WorstBest” fragment.
But in this case when

    public void prendiLista(ArrayList<Stock> al){
        lt=al;
         myAdap.notifyDataSetChanged();
    }    

the view is updated !!!

Please what is the difference??? 🙂

IN MY UNDERSTANDING THE CALL TO notifyDataSetChanged() WAS ENOUGH TO UPDATE THE VIEW !!

Thanks a lot anyway! (I will accept answer tomorrow morning).

 public class listafragment extends Fragment {
    public ListView lv;
    public MyAdapter myAdap;
    public ArrayList<Stock> lt;
    public View v;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) {
         // Inflate the layout for this fragment
        View v= inflater.inflate(R.layout.lista, container, false);    
        lv= (ListView) v.findViewById(android.R.id.list);
        if(lv==null){Log.e("","nel fragment la lista è nulla");}
         if(lv!=null){Log.e("","nel fragment la lista NON è nulla");} 
         myAdap= new MyAdapter(lt, getActivity()); 
         lv.setAdapter(myAdap);             
        return v;
    }

    public void prendiLista(ArrayList<Stock> al){
        Log.e("", "passo per prendilitsa del fragment");
        lt=al;
        if(myAdap==null){Log.e("","nel prendilista del fragment myadapt è     nullo");};
        myAdap.notifyDataSetChanged();
    }                 
       public void addToFavorites(int pos){
         Quotes.addToFavorites(pos);

     }

     @Override
    public void onCreate(Bundle savedInstanceState){
        lt=Quotes.dammi();
        super.onCreate(savedInstanceState);
    }
}
  • 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-16T19:23:39+00:00Added an answer on June 16, 2026 at 7:23 pm

    therefore the lists should update with the new contents of the ArrayLists.

    Unfortunately no. When your app first runs l and best both reference the same data. You have this set up.

    Fragment:
        best ---\
                |
                |--- <the initial data>
    Adapter:    |
        l ------/
    

    But after you call best=al, you have:

    Fragment:
        best ------- <the new data>
    
    Adapter:    
        l ---------- <the initial data>
    

    So you never update the reference in your Adapter. Either add a new method to your custom Adapter to change l or use l itself since you didn’t declare it as private:

    public void prendiListaBest(ArrayList<Stock> al){
        myAdap.l=al;
        myAdap.notifyDataSetChanged();
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to following code fragment, and no matter what I set the font-size
I have the following code that seems like it should set my insertParameter but
How to set transparency of an rectangle/screen. I have Following code: // main.cpp void
I have a draggable view that I have set up with the following code:
I have following code in my application: // to set tip - photo in
I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS
I have the following code to set a userId variable: (userId set in prior
I have the following code: UPDATE myTable SET Col1 = @Value However, I have
I have the following code set up in my Startup IDictionary<string, string> properties =
I have used the following code to set cookie and then redirect. String level=(String)

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.