2014-03-05 2 views
0

내 이름은 Luis이고 프로그래밍의 세계에서 시작하고 있습니다.onContextItemSelected 제거

지금 테스트 단계에 있습니다. 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(); 
     } 
    } 
} 
+0

무엇이 문제입니까? 너의 문제는 뭐니? – Merlevede

+0

listView에서 onContextItemSelected를 사용하여 항목을 삭제해야합니다. – boxer609

답변

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(); 

그러면 목록이 업데이트됩니다.

+0

고맙습니다. 편집하려면 어떻게해야합니까? – boxer609

+0

같은 방법으로'results' 배열을 수정하면 완료되면'notifyDatasetchanged()'를 호출합니다 – Merlevede

+0

caso R.id.menu_editar : this.results.add (info.position, null); 새로운 배열 어댑터 setListAdapter (mAdapter1); mAdapter1.notifyDataSetChanged(); 참을 돌려라. 내가 뭔가 잘못한 것 같아? – boxer609

관련 문제