如何从mainactivity中检测listview中的更改

时间:2013-12-17 13:24:18

标签: android listview adapter

摘要:在我的ListView中添加或删除项目时,我需要一种在主要活动中触发compute()函数的方法

背景: 我的Android应用程序用列表项填充列表视图。列表项包含文本视图和图像按钮(删除),用于在单击时从列表中删除项目。我使用自定义适配器来跟踪列表中的更改。一切正常。

在我的主要活动中,一些计算基于名为calulate()的函数中列表中的值进行。我想在列表中添加或删除项目时调用此函数。但是,我不知道这是否可行,以及如何实现这样的功能。

我注意到可以使用registerDataSetObserver()添加一个观察者,当调用notifyDataSetChanged()时会通知它。但是,我不确定这是否是我需要的以及如何实现这一点。任何帮助或建议都非常受欢迎。

这是我的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");
    }


}

以下是我在onCreate()方法中初始化ListView的方法。

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

如果需要任何其他代码片段,我很乐意提供它。

2 个答案:

答案 0 :(得分:1)

您可以创建将由您的主要活动实施并传递给适配器的Interfece(例如,在构造函数中)

public interface SomeInterface

    {
        public void foo();
    }

在适配器中添加SomeInterface对象

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();
    }

并在MainActivity中实现SomeInterface

public class MainActivity extends Activity implements SomeInterface
{

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

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

答案 1 :(得分:0)

您可以使用一个简单的方法创建回调接口,例如stuffHappened()。然后,让您的活动实现该接口。现在,您可以添加一个构造函数参数,该参数具有回调接口的类型,传递活动,将其作为成员变量保留在适配器上,并在需要向您的活动发送反馈时调用stuffHappened()方法。