0

我正在从自定义微调器打开一个对话框,让我们说这个对话框中的对话框-A-A 有一个 listView 和一个按钮。当我单击对话框-A 上的按钮时,另一个对话框-B 已打开,我做了一些工作并按下对话框-B 上的按钮,并且对话框-B 在关闭此对话框-B 后关闭,我想更新对话框-A 中的列表视图。

我的问题是我应该调用哪个事件以便我可以在对话框-A 中更新我的 listView?

如果有些事情不清楚,请告诉我。

以下是我打电话的代码

查看第一个代码块中的行: btnManageCategory.setOnClickListener(new ManageCategory(context));
请考虑 ManageCategory 是对话框-A。请参阅第二个代码块中的行: btnAddCategory.setOnClickListener(new AddCategory(context)); 请考虑 AddCategory 是对话框-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();

在对话框关闭之前

这就是我解决问题的方法。任何人都可以通过我的代码并对其进行审查,并建议我如何改进它。

4

2 回答 2

1

您可以在对话框关闭事件发生时更新您的列表视图。您可以通过使视图无效来更新您的列表。

于 2013-09-17T12:42:20.233 回答
0

这是一个适合我的解决方案。

于 2014-03-16T05:23:54.803 回答