访问setPositiveButton OnClick函数中的公共数组列表

时间:2014-02-22 10:25:15

标签: android android-arrayadapter android-alertdialog android-contextmenu

我目前在public array list功能中访问setPositiveButton onClick时遇到问题。用它访问它不起作用。这是我的代码:

使用标题模块声明数组列表。

public class DisplayModulesActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {

    ArrayList<Modules> modules;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_modules);

        modules = new ArrayList<Modules>();
        modules.add(new Modules("387COM Smartphone App Development"));

        ListView listView = (ListView) findViewById(R.id.moduleList);

        adapter = new ArrayAdapter<Modules>(this,
                android.R.layout.simple_list_item_1, modules);

        listView.setAdapter(adapter);
        registerForContextMenu(listView);
        listView.setOnItemClickListener(this);
    }
}

我想在这里访问数组列表并从中删除一些内容(与modules.remove(i)一致; 它不起作用。

public boolean onContextItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.delete_module) {
        AlertDialog.Builder confirmDel = new AlertDialog.Builder(this);
        confirmDel
                .setMessage("Are you sure you want to delete this module and all its contents?");
        confirmDel.setCancelable(true);
        confirmDel.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface,
                            int i) {
                        Log.v("Test", "Confirm Delete YES pressed");
                        // AdapterView.AdapterContextMenuInfo info =
                        // (AdapterView.AdapterContextMenuInfo)
                        // item.getMenuInfo();
                        // int i = info.position;
                        modules.remove(i);
                        // adapter.notifyDataSetChanged();
                    }
                });
        confirmDel.setNegativeButton("No", null);

        confirmDel.show();
    }
}

有什么想法吗?

FIX

public boolean onContextItemSelected(final MenuItem item)
{
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    final int arrayItem = info.position;

    if(item.getItemId() == R.id.delete_module)
    {
        AlertDialog.Builder confirmDel = new AlertDialog.Builder(this);
        confirmDel.setMessage("Are you sure you want to delete this module and all its contents?");
        confirmDel.setCancelable(true);
        confirmDel.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Log.v("Test", "Confirm Delete YES pressed");

                modules.remove(arrayItem);
                adapter.notifyDataSetChanged();
            }
        });
        confirmDel.setNegativeButton("No", null);

        confirmDel.show();
    }

2 个答案:

答案 0 :(得分:0)

只需在代码中添加:

         adapter = new ArrayAdapter<Modules>(this,android.R.layout.simple_list_item_1,modules);

         listView.setAdapter(adapter);

这样做;)

public boolean onContextItemSelected(MenuItem item)
{
if(item.getItemId() == R.id.delete_module)
{
    AlertDialog.Builder confirmDel = new AlertDialog.Builder(this);
    confirmDel.setMessage("Are you sure you want to delete this module and all its contents?");
    confirmDel.setCancelable(true);
    confirmDel.setPositiveButton("Yes", new DialogInterface.OnClickListener()
    {
        @Override
        public void onClick(DialogInterface dialogInterface, int i)
        {
            Log.v("Test", "Confirm Delete YES pressed");
            //AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
            //int i = info.position;
            modules.remove(i);
             adapter = new ArrayAdapter<Modules>(this,android.R.layout.simple_list_item_1,modules);

             listView.setAdapter(adapter);
        }
    });
    confirmDel.setNegativeButton("No", null);

    confirmDel.show();
}

答案 1 :(得分:0)

问题是,i不是position ArrayList中项目的modules。 它是正面按钮的position(每次点击可能是0或1或2,无论是哪个图标;也许它都是-1)。

要解决此问题,请执行以下操作:

保存/使用position中提供的onItemClick(AdapterView<?> parent, View view, int position, long id),并以某种方式将其传递到对话框的onClickListener()。使用此位置可从modules列表中删除项目。

E.g。在onItemClick()中将position保存到您的班级mClickedPosition的字段中,并在听众中拨打modules.remove(mClickedPosition)