从其他活动的列表中删除项目

时间:2014-03-26 16:17:27

标签: android listview button

如何从另一个活动的按钮中移除ListActivity中的项目,事情是,

我有ListActivity

public class ListaEventos extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onRestart();
        republicar();
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.criar_evento:
                Intent criar = new Intent(this, CriarEvento.class);
                startActivity(criar);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void republicar() {
        List<DadosEvento> eventos = MySingleton.getInstance().getEventos();
        setListAdapter(new ArrayAdapter<DadosEvento>(this, android.R.layout.simple_list_item_1, eventos));
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {

        DadosEvento clickNumber = MySingleton.getInstance().getEventos().get(position);

        Intent intent = new Intent(this, ExibirEvento.class);
        Bundle exibirEvento = new Bundle();
        exibirEvento.putString("exibirNome", clickNumber.getNome());
        exibirEvento.putString("exibirData", clickNumber.getData());
        exibirEvento.putString("exibirEnd", clickNumber.getEndereco());
        exibirEvento.putString("exibirTel", clickNumber.getTel());
        intent.putExtras(exibirEvento);
        startActivity(intent);
    }
}

其他活动:

public class ExibirEvento extends Activity {

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

        Intent intent = getIntent();
        Bundle exibirEvento = intent.getExtras();

        String exibirNome = exibirEvento.getString("exibirNome");
        String exibirData = exibirEvento.getString("exibirData");
        String exibirEnd = exibirEvento.getString("exibirEnd");
        String exibirTel = exibirEvento.getString("exibirTel");

        TextView exibir_nome = (TextView) findViewById(R.id.exibirevento_edittext_nome);
        exibir_nome.setText(exibirNome);

        TextView exibir_data = (TextView) findViewById(R.id.exibirevento_edittext_data);
        exibir_data.setText(exibirData);

        TextView exibir_end = (TextView) findViewById(R.id.exibirevento_edittext_end);
        exibir_end.setText(exibirEnd);

        TextView exibir_tel = (TextView) findViewById(R.id.exibirevento_edittext_tel);
        exibir_tel.setText(exibirTel);
    }

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

    public void sairExibicao(View v) {
        finish();
    }
}

现在,在ExibirEvento上它显示了关于Party的信息,在那里我需要一个按钮从列表中删除该项目。

1 个答案:

答案 0 :(得分:1)

您可以遵循以下几种方案。

  1. 使用startActivityForResult代替startActivity,然后覆盖onActivityResult方法并抓住正确的requestCoderesult(例如RESULT_OK })并删除当前项和notifyDataChanged

  2. adapter
  3. 使用singelton类,创建newInstance,设置值,然后从ExibirEvento

  4. 引用它
  5. 使用数据库,并利用ContentProviderLoader,并仅将条目的ID传递给ExibirEvento

  6. 我认为3选项是最佳选择。