我想要的是,解决一个新的活动是考虑到名称而不是位置,例如这里我有两个名字“Ciclismo”和“Correr”,这里代码:
public class MainActivity extends Activity {
ListView lista;
ArrayList<Datos> arraydatos = new ArrayList<Datos>();
Datos datos;
adapterdatos adapter = new adapterdatos(this, arraydatos);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lista = (ListView) findViewById(R.id.listView_lista);
datos = new Datos(getResources().getDrawable(R.drawable.ciclistapng), "Ciclismo", "Imagen de tipo PNG");
arraydatos.add(datos);
datos = new Datos(getResources().getDrawable(R.drawable.corredor), "Correr", "Imagen de tipo PNG");
arraydatos.add(datos);
lista.setAdapter(adapter);
registerForContextMenu(lista);
}
@Override
public void onCreateContextMenu (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MainActivity.super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_context_menu, menu);
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent ci = new Intent(getApplicationContext(), ciclismo.class);
startActivity(ci);
Toast.makeText(getApplicationContext(), "Ciclismo", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "Correr", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete_id:
arraydatos.remove(info.position);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),"Eliminado", Toast.LENGTH_SHORT).show();
return true;
case R.id.añadir_id:
arraydatos.add(datos);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(),"Añadido", Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onContextItemSelected(item);
}
}
}
我希望它不是因为它的位置而是它在listview中的名称。谢谢你,原谅我的英语。
答案 0 :(得分:0)
我所理解的是,当在ListView中单击某个项目时,您希望通过名称而非位置来获取它。纠正我,如果错了。 ListView点击通常是通过位置获得的,但您可以通过在listview项目中设置标记来获取视图名称。