概要:アイテムがリストビューに追加またはリストビューから削除されたときに、メインアクティビティ内でcalculate()関数をトリガーする方法が必要です
背景:私のAndroidアプリケーションは、リストビューをリストアイテムで埋めます。リストアイテムには、クリックするとリストからアイテムを削除するテキストビューとイメージボタン(削除)が含まれます。リストの変更を追跡するためにカスタムアダプタを使用しています。これはすべて正常に機能します。
私の主な活動では、calulate()と呼ばれる関数のリスト内の値に基づいていくつかの計算が行われます。アイテムがリストに追加またはリストから削除されるたびに、この関数を呼び出したいと思います。しかし、これが可能かどうか、そしてそのような機能をどのように実装するかはわかりません。
notifyDataSetChanged()が呼び出されたときに通知されるregisterDataSetObserver()を使用してオブザーバーを追加できることに気付きました。しかし、これが私が必要としているものであるかどうか、そしてこれをどのように実装するかはわかりません。どんな助けや提案も大歓迎です。
これが私の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");
}
}
これが私のListViewが私のonCreate()メソッドでどのように初期化されるかです。
selectionListView = (ListView) findViewById(R.id.selectionListView);
selectionAdapter = new CustomListAdapter(this,myListItems);
selectionListView.setAdapter(selectionAdapter);
他のコードフラグメントが必要な場合は、喜んで提供します。
メインアクティビティによって実装され、アダプタに渡されるInterfeceを作成できます(コンストラクターなど)
public interface SomeInterface
{
public void foo();
}
アダプタにSomeInterfaceオブジェクトを追加します
SomeInterfaceレスポンダー= 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);
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加