删除onContextItemSelected

时间:2014-03-05 16:42:30

标签: java android

我的名字是路易斯,我从编程世界开始。

现在我正处于测试阶段。我正在尝试删除和修改在ListView中创建的项目。

public class TravelActivity extends ListActivity {

    private static final int REQUEST_CODE_AGREGAR_VIAJE = 100;

    private List<String> results = new LinkedList<String>();
    ListView list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        // results=new LinkedList<String>();
        list = (ListView) findViewById(android.R.id.list);
        registerForContextMenu(list);// No me deja no entiendo el porque


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.opciones, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getItemId()) {
        case R.id.anadir_viaje:
            // Creamos el Intent para lanzar la Activity EditTravelActivity
            Intent intent = new Intent(this, EditTravelActivity.class);

            startActivityForResult(intent, REQUEST_CODE_AGREGAR_VIAJE);
            return true;
        case R.id.compartir:
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, toString());
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
            break;

        default:
            Toast.makeText(this, "Invalid option", Toast.LENGTH_LONG).show();
            break;
        }

        return super.onOptionsItemSelected(item);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_menu_item_longpress, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // AdapterContextMenuInfo info=
        // (AdapterContextMenuInfo)item.getMenuInfo();
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
                .getMenuInfo();

        switch (item.getItemId()) {
        case R.id.menu_delete:

            this.results.remove(info.position);
            return true;
        default:

            return super.onContextItemSelected(item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_CODE_AGREGAR_VIAJE
                && resultCode == RESULT_OK) {
            String pais, ciudad, ano, anotacion;
            pais = data.getStringExtra(EditTravelActivity.VALOR_PAIS);
            ciudad = data.getStringExtra(EditTravelActivity.VALOR_CIUDAD);
            ano = data.getStringExtra(EditTravelActivity.VALOR_ANO);
            anotacion = data.getStringExtra(EditTravelActivity.VALOR_ANOTACION);
            Viaje viaje = new Viaje(pais, ciudad, ano, anotacion);

            results.add(viaje.toString());
            ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
        } else {
            Toast.makeText(this, R.string.cancel, Toast.LENGTH_LONG).show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,我建议将适配器放在变量中:

ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results);
setListAdapter(mAdapter);

执行此行后

this.results.remove(info.position);

您需要通知适配器数据集已更改,您可以使用

mAdapter.notifyDatasetChanged();

这将更新列表。