1
I’m building my app in android studio, I created a normal acitivity that has actionbar support, in this acitivity I load a list of users but I can not fire these events, how to solve this? My list is mounted through a basedapter.
public class ListUsuariosActivity extends ActionBarActivity
implements AdapterView.OnItemClickListener, DialogInterface.OnClickListener{
private ListView lista;
private List<Usuario> usuarios;
private UsuarioAdapter adapter;
private UsuarioDAO helper;
private int idposicao;
private AlertDialog alertDialog, dialogConfirmacao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_usuarios);
alertDialog = Mensagem.criarAlertDialog(this);
dialogConfirmacao = Mensagem.criarDialogConfirmacao(this);
helper = new UsuarioDAO(this);
usuarios = helper.listarUsuarios();
adapter = new UsuarioAdapter(this, usuarios);
//Montar o listview
lista = (ListView) findViewById(R.id.lvUsuarios);
lista.setAdapter(adapter);
/*lista.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//idposicao = position;
alertDialog.show();
}
});
lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});*/
}
@Override
protected void onDestroy() {
helper.fechar();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.list_usuarios, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_usuario_sair:
Helper.sairForm(this);
break;
case R.id.action_lista_usuario_add:
startActivity(new Intent(this, UsuariosActivity.class));
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(DialogInterface dialog, int which) {
Mensagem.addMsg(this, "Teste");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Mensagem.addMsg(this, "Teste");
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#FFF">
<ListView
android:id="@+id/lvUsuarios"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/usuarios_lista_nome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/usuarios_lista_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13sp"
android:textStyle="italic"
android:layout_marginBottom="10dp"/>
I did it this way but it’s a mistake, or I’m not knowing how to implement it in the right way, which is most likely.
– Thiago Porto
Which error is shown?
– Wakim
None, but you don’t have the code add-on, you could make an example?
– Thiago Porto
@Thiagoporto, updated the answer with an example. If you have an error, check the
StackTrace
and let me know.– Wakim
Everything worked out, thank you.
– Thiago Porto