我从自定义微调器打开一个对话框,让对话框-A在这个对话框中-A有一个listView和一个按钮。当我点击对话框上的按钮时 - 另一个对话框-B打开我做了一些工作并按下对话框B上的按钮,关闭此对话框后关闭对话框-B 我想在对话框A中更新listView。
我的问题是我应该调用哪个事件,以便我可以在对话框A中更新我的listView?
如果有些事情不清楚,请告诉我。
以下是我打电话的代码
查看第一个代码块中的行:
btnManageCategory.setOnClickListener(new ManageCategory(context));
请考虑ManageCategory是dialog-A。
看到第二个代码块中的行:
btnAddCategory.setOnClickListener(new AddCategory(context));
请注意AddCategory是dialog-B。
public class SpinnerDialog extends AlertDialog implements OnItemClickListener, View.OnClickListener {
DialogInterface.OnClickListener mListener;
OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
View promptsView;
Context mContext;
ListView lList;
protected SpinnerDialog(Context context, int inPosition, ArrayAdapter<Accounts> inSpinnerAdapter, DialogInterface.OnClickListener inListener) {
super(context);
// TODO Auto-generated constructor stub
promptsView = getLayoutInflater().inflate(R.layout.activity_spinner_dialog, null);
mListener = inListener;
mContext = context;
lList = (ListView)promptsView.findViewById(R.id.listCategory);
//lList.setBackgroundColor(color.white);
lList.setAdapter(inSpinnerAdapter);
lList.setOnItemClickListener(this);
lList.setChoiceMode(ListView.CHOICE_MODE_SINGLE );
lList.setItemChecked(inPosition, true);
Button btnManageCategory = (Button)promptsView.findViewById(R.id.btnManageCategory);
btnManageCategory.setOnClickListener(new ManageCategory(context));
this.setView(promptsView);
}
@Override
public void onClick(View v) {
if(mListener != null)
mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mListener != null)
mListener.onClick(SpinnerDialog.this, position);
//String text = parent.getItemAtPosition(position).toString();
onItemSelectedListener.onItemSelected(parent, view, position, id);
}
}
Dailog-A代码:
public class ManageCategory implements android.view.View.OnClickListener {
public Context context = null;
ListView myList;
CategoryListAdapter adapter;
//ArrayAdapter<Category> adapter;
public ManageCategory(Context context) {
this.context = context;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(this.context);
dialog.setContentView(R.layout.manage_category);
dialog.setTitle("Manage Category");
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// TODO Auto-generated method stub
Log.i("onShow", "now it is called");
}
});
Button btnAddCategory = (Button)dialog.findViewById(R.id.btnAddCategoryy);
btnAddCategory.setOnClickListener(new AddCategory(context));
getAllCategory(dialog.findViewById(R.id.listManageCategory));
dialog.show();
}
public void getAllCategory(View v) {
AccountDS datasource;
datasource = new AccountDS(context);
datasource.open();
List<Category> values = datasource.getAllCategoryList();
datasource.close();
adapter = new CategoryListAdapter(context, R.layout.category_list, values);
myList=(ListView)v;
if (myList == null) {
Log.i(ManageCategory.class.getName(), "List View is null ");
}
if (adapter == null)
Log.i(ManageCategory.class.getName(), "adaptor is null ");
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(context,"Click ListItem Number " + arg2, Toast.LENGTH_LONG).show();
}
});
}
}
Dailog-B代码:
public class AddCategory implements android.view.View.OnClickListener {
public Context context = null;
ListView myList;
CategoryListAdapter adapter;
public AddCategory(Context context) {
this.context = context;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(this.context);
final Context c = this.context;
dialog.setContentView(R.layout.add_category);
dialog.setTitle("Add Category");
Button btnAdd = (Button)dialog.findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText txtCategoryName = (EditText)dialog.findViewById(R.id.txtCategoryName);
Log.i("Add Category", txtCategoryName.getText().toString());
AccountDS datasource;
datasource = new AccountDS(c);
datasource.open();
datasource.createCategory(new Category(0, txtCategoryName.getText().toString()));
datasource.close();
dialog.dismiss();
}
});
Button btnClose = (Button)dialog.findViewById(R.id.btnClose);
btnClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
}
在ManageCategory类中添加静态方法
public static void updateData(){
AccountDS datasource;
datasource = new AccountDS(context);
datasource.open();
List<Category> values = datasource.getAllCategoryList();
datasource.close();
adapter = new CategoryListAdapter(context, R.layout.category_list, values);
if (myList == null) {
Log.i(ManageCategory.class.getName(), "List View is null ");
}
if (adapter == null)
Log.i(ManageCategory.class.getName(), "adaptor is null ");
myList.setAdapter(adapter);
}
使myList,适配器和上下文也是静态的并调用
ManageCategory.updateData();
在对话解雇之前
这就是我解决问题的方法。任何人都可以通过我的代码审查它,并建议我如何改进它。