views:

84

answers:

2

Hi!

I have a listview with a customized ArrayAdapter. When the in the ArrayAdapter has changed the gui is supposed to update. However, when it's supposed to update, it won't unless you press a key on the emulator. The clickable rows are not clickable either unless i have clicked on an arbitrary button on the emulator. The underlying data for the listview is changed somewhere else and then notifying the observer (the listview in this case)

public void update(java.util.Observable arg0, Object arg1) 
    {
        Log.e("Supposed","to update");
        ad.notifyDataSetChanged(); //Ad is the arrayadapter                         
    }

The data is presented properly after the magic touch on the buttons. But why? Any thoughts? When i inspect the ArrayAdapter's items-reference it shows the updated values, but the gui wont. Plz help

edit:

private class MustAdapter extends ArrayAdapter<Item> 
    {
        public MustAdapter(Context context, int textViewResourceId, ArrayList<Item> items)
        {
            super(context,textViewResourceId,items);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.row, null);
                }

                Item o = items.get(position);
                if (o != null) {

                        TextView tt = (TextView) v.findViewById(R.id.text1); 
                        TextView bt = (TextView) v.findViewById(R.id.text2);
                        ImageView iv= (ImageView) v.findViewById(R.id.img);
                        ImageView ib = (ImageView)v.findViewById(R.id.OnOffButt);
                        if (ib != null) {
                            ib.setTag(position);
                            if (((Ventilation)o).enabled ==0 ) {
                                ib.setImageResource(android.R.drawable.ic_input_delete);
                            }
                            else
                                ib.setImageResource(android.R.drawable.ic_input_add);
                            ib.setOnClickListener(new OnClickListener() {

                            public void onClick(View arg0) {
                                int index=((Integer)arg0.getTag()).intValue();
                                Ventilation v= (Ventilation)items.get(index);
                                v.enabled=v.enabled != 0 ? 0 : 1;
                                //ad.notifyDataSetChanged();
                                DU.toggle(v);


                            }
                        });
                        }

                        if (iv !=null){
                            iv.setImageResource(R.drawable.fan);
                        }
                        if (tt != null) {
                              tt.setText(""+((Ventilation)o).actual);                            }
                        if(bt != null){
                              bt.setText(""+((Ventilation)o).setPoint);
                        }
                }
                return v;
        }

Even when i use arrayadapter.add(item) i have to press somewhere else for it to reload

A: 

When the in the ArrayAdapter has changed the gui is supposed to update.

If will, if you change the ArrayAdapter's data via add(), insert(), or remove(). If you change the data some other way, ArrayAdapter will not know about the change.

CommonsWare
Even arrayadapter.add(Item) needs an press at any button to respond to changes.
Raul
@Raul: no, it does not. See http://github.com/commonsguy/cw-android/tree/master/Threads/Asyncer/ and http://github.com/commonsguy/cw-andtutorials/tree/master/04-ListView/
CommonsWare
Guess my emulator does not work then. I'll try on ubuntu in a few days
Raul
A: 

Ok, so the thing is that no thread but the gui thread can update the gui thread. So what i did was to declare a Handler in the onCreate event.

h= new Handler(){
                @Override
                public void handleMessage(Message msg)
                {
                    switch (msg.what)
                    {
                    case 0:
                        ad.notifyDataSetChanged();
                    }
                }
            };

and in my public void update(java.util.Observable arg0, Object arg1)

i put

Message ms= new Message();
        ms.what=0;
        h.sendMessage(ms);

Thats it! Hope this one comes handy for someone.

Raul