Using the onclick and onitemclick methods in ActionBarActivity

1

I'm building my app in android studio, I created a normal acitivity that has actionbar support, in that acitivity I load a list of users however I can not fire these events, how to solve this? My list is assembled through a baseadapter.

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

    
asked by anonymous 16.10.2014 / 04:18

1 answer

1

You forgot to add your ListUsuariosActivity to listen for the events of itemClick and click in ListView , just as you did the first time.

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

In addition, if the goal is just to listen for clicks on items in the list, I believe OnClick is not required.

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.

}
    
16.10.2014 / 12:45