2

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);
    }
}
Lisa Anne
  • 4,479
  • 13
  • 70
  • 146
  • 1
    It looks like you have custom adapters. Can you please post the code of those classes? Perhaps you are not implementing notifyDataSetChanged correctly. – Ameen Jan 02 '13 at 17:50

1 Answers1

1

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();
}    
Sam
  • 84,460
  • 18
  • 172
  • 171
  • 1
    answer is correct but a more encapsulated way would be to use a public method of the adapter to refill its internal list : http://stackoverflow.com/a/12662994/693752 – Snicolas Jan 02 '13 at 18:24
  • 1
    I agree and suggested the same technique in my answer. I am hoping this code demonstrates how exposed `l` is, but I felt the answer was long enough already... – Sam Jan 02 '13 at 18:37
  • @Sam Thanks a lot! But please would you mind having a look at my edits, I have some perplexities,although your answer makes perfectly sense? (I will accept the answer tomorrow morning anyway). Thanks Again. – Lisa Anne Jan 02 '13 at 20:49
  • 1
    @pietmau That is confusing. Are you changing `Quotes.dammi()` somewhere else? – Sam Jan 02 '13 at 22:15
  • @Sam Thanks Sam. I am invoking the main Activity method `lt=Quotes.dammi()` to retrieve the data set at the beginning (it is in onCreate...) after that, when the dataset changes the main activity calls `prendiLista(ArrayList al)` to notify that the data are changed. And it works... thanks again – Lisa Anne Jan 03 '13 at 07:59
  • can any one check http://stackoverflow.com/questions/28148618/listview-not-refreshing-after-click-on-button –  Jan 27 '15 at 13:22