Using onclick and onitemclick methods on Actionbaractivity

Asked

Viewed 1,438 times

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"/>

1 answer

1


Forgot to add your ListUsuariosActivity to listen to the events of itemClick and click in ListView, just like you did the first time.

Just uncomment the code and replace the new View.OnClickListener... and the new AdapterView.OnItemClickListener for this.

Also, if the goal is just to listen for clicks on list items, I believe that the OnClick it is not necessary.

An example would be:

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

        // Seta a propria Activity como o Listener
        lista.setOnItemClickListener(this);
    }

    // Restante do seu codigo.

}
  • 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.

  • Which error is shown?

  • None, but you don’t have the code add-on, you could make an example?

  • @Thiagoporto, updated the answer with an example. If you have an error, check the StackTrace and let me know.

  • Everything worked out, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.