Frank D.
Frank D.

Reputation: 1306

How to detect change in listview from mainactivity

Summary: I need a way to trigger my calculate() function within my main activity when an item is added or removed from my ListView

Background: My android application fills a listview with list items. A list item contains a textview and an imagebutton (delete) that removes the item from the list on click. I use a custom adapter to keep track of changes in the list. This all works fine.

In my main acticity, some calculations take place based on the values in the list in a function called calulate(). I want to call this function whenever an item is added or deleted from the list. However, I don't know if this is possible and how to implement such a function.

I noticed that it is possible to add an observer using registerDataSetObserver() that will be notified when notifyDataSetChanged() is called. However, I'm not sure if this is what I need and how to implement this. Any help or suggestions are more than welcome.

Here is my CustomListAdapter:

public class CustomListAdapter extends BaseAdapter {         

static final String TAG = "CustomListAdapter";
private Context context;
ArrayList <String> listArray;
LayoutInflater inflater;

public CustomListAdapter(Context context, List <String> inputArray) {                                                        
    super();
    this.context = context;
    this.listArray = (ArrayList<String>) inputArray;
}                                                                           


 @Override
    public int getCount() {
        return listArray.size();    // total number of elements in the list
    }

    @Override
    public String getItem(int i) {
        return listArray.get(i);    // single item in the list
    }

    @Override
    public long getItemId(int i) {
        return i;                   // index number
    }

@Override                                                                   
public View getView(int position, View convertView, final ViewGroup parent) {
     View V = convertView;

     if(V == null) {
            LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            V = vi.inflate(R.layout.selected_drug_list_item, null);
        }

     //place text in textview
    String listItem = listArray.get(position);
    TextView textView = (TextView) V.findViewById(R.id.selectedDrugName);
    textView.setText(listItem);        

    ImageButton deleteSelectedDrugButton = (ImageButton) V.findViewById(R.id.deleteSelectedDrugButton);
    deleteSelectedDrugButton.setTag(position);


    //Listener for the delete button. Deletes item from list.

    deleteSelectedDrugButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
             //re
             Integer index = (Integer) view.getTag();
             listArray.remove(index.intValue());
             notifyDataSetChanged();
        }
    });


    return V;
}


public void add(String input) {
    listArray.add(input);
    notifyDataSetChanged();
    Log.v(TAG, input + " added to list");
}

public void remove(String input){
    listArray.remove(input);
    notifyDataSetChanged();
    Log.v(TAG, input + " added to list");
    }


}

Here is how my ListView is initialized in my onCreate() method.

selectionListView = (ListView) findViewById(R.id.selectionListView);
selectionAdapter = new CustomListAdapter(this,myListItems);
selectionListView.setAdapter(selectionAdapter);

If any other code fragment is required, I'll happily provide it.

Upvotes: 0

Views: 2209

Answers (2)

Gooziec
Gooziec

Reputation: 2776

You may create Interfece that will be implemented by Your Main Activity and passed to Adapter (eg. in constructor)

public interface SomeInterface

    {
        public void foo();
    }

Add SomeInterface object in Your Adapter

SomeInterface responder=null;

public CustomListAdapter(Context context, List <String> inputArray, SomeInterface responder) {                                                        
    super();
    this.context = context;
    this.listArray = (ArrayList<String>) inputArray;
    this.responder=responder;
}       

public void add(String input) {
    listArray.add(input);
    notifyDataSetChanged();
    Log.v(TAG, input + " added to list");
    responder.foo();
}

public void remove(String input){
    listArray.remove(input);
    notifyDataSetChanged();
    Log.v(TAG, input + " added to list");
    responder.foo();
    }

and implements SomeInterface in Your MainActivity

public class MainActivity extends Activity implements SomeInterface
{

...
 public void foo()
{
//do whatever
}

private initializeAdapter()
{
CustomListAdapter adapter=new Adapter(this, someArray, this);
}
}

Upvotes: 2

Toon Borgers
Toon Borgers

Reputation: 3658

You can create a callback interface with a simple method, like stuffHappened(). Then, let your activity implement that interface. Now you can add a constructor argument which has as type the callback interface, pass the activity in, keep it as a member variable on the adapter and call the stuffHappened() method whenever you need to send feedback to your activity.

Upvotes: 0

Related Questions