John
John

Reputation: 8946

updating the correct list data in listview when an event occurs

I have a ListView which shows a list of objects for example CustomObject.

The list view is displayed fine.

I have a Callback listener in each custom Object, to update a boolean member in the CustomOject.

public class ListCustomObject extends Activity {
private CustomListAdapter adapter;
private ListView customList;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  adapter = new CustomListAdapter (this);
  customList= (ListView) findViewById(R.id.custom_list);
  List<CustomObject> customObjectList= getCustomObjects();
  adapter.addObjects(customObjectList);
  customList.setAdapter(adapter);
}

class CustomObject implements MonitoringListner{
     String name;
     boolean entered;

     @Override
     public void onEnteredRegion() {
        entered=true;

    }

    @Override
    public void onExitedRegion() {
        entered=false;
    }
}

i am displaying both 'Name' and 'entered' in the listItem.

The problem is, when the callback arrives and changes the boolean value to true. Its not reflecting in the listview.

How to update the corresponding list item when callback event arrives for corresponding object?.

Please help.

Awaiting for some answers.

Upvotes: 0

Views: 217

Answers (1)

Emran Sadeghi
Emran Sadeghi

Reputation: 612

use this code to notify listview

public class ListCustomObject extends Activity {
private CustomListAdapter adapter;
private ListView customList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    adapter = new CustomListAdapter (this);
    customList= (ListView) findViewById(R.id.custom_list);
    List<CustomObject> customObjectList= getCustomObjects();
    adapter.addObjects(customBeaconList);
    customList.setAdapter(adapter);
}

class CustomObject implements MonitoringListner{
    private CustomListAdapter adapter;
    public CustomObject(CustomListAdapter adapter) {
        this.adapter = adapter;
    }
    String name;
    boolean entered;

    @Override
    public void onEnteredRegion() {
        entered=true;
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onExitedRegion() {
        entered=false;
        adapter.notifyDataSetChanged();
    }
}

and when you want create CustomObject pass adapter to constructor.

Upvotes: 1

Related Questions