setOnItemClickListener not working when clicking on a Listview line

Asked

Viewed 140 times

1

I’m beginner in android development, and I came across a problem that I can’t solve. Well, I put a listview within a view_pager (content_main) which is included in activity_main. When I click on a list item, it will call a method that will open a new one Activity. Before you put that listview within that new layout, was working normally. After I used a view_pager (content_main), toolbar, tablayout, coordinatorLayout (activity_main), to create tabs, the event setOnItemClickListener doesn’t want to work. What do I do? I’ll leave the code...

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.deniswilson.itabus.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent">


    </android.support.v4.view.ViewPager>
</RelativeLayout>

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.deniswilson.itabus.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

        <android.support.design.widget.TabLayout
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/tab_layout">

        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email"/>

</android.support.design.widget.CoordinatorLayout>

Listiview screen

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:inputType="textPersonName"
        android:hint="Pesquisar"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="14dp"
        android:layout_marginStart="14dp"
        android:layout_marginTop="23dp"
        android:id="@+id/pesq_municipal"
        android:layout_height="50dp"
        android:layout_width="270dp"/>

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_marginRight="12dp"
        android:layout_marginEnd="12dp"
        android:id="@+id/imgBotaoI"
        android:layout_alignTop="@+id/pesq_intermunicipal"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/imgBotaoI"
        android:layout_alignLeft="@+id/pesq_intermunicipal"
        android:layout_alignStart="@+id/pesq_intermunicipal"
        android:layout_marginTop="31dp"
        android:id="@+id/listarMunicipal"/>

</RelativeLayout>

Java Class with Event

import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.*;

import com.example.deniswilson.itabus.Administrador.ActCadMunicipal;
import com.example.deniswilson.itabus.Administrador.BD;
import com.example.deniswilson.itabus.Administrador.Interacoes;
import com.example.deniswilson.itabus.Administrador.Municipal;
import com.example.deniswilson.itabus.MainActivity;
import com.example.deniswilson.itabus.MainActivityInfo;
import com.example.deniswilson.itabus.R;
import com.example.deniswilson.itabus.Tabs.InfoUsuarios;

/**
 * Created by Denis Wilson on 12/10/2016.
 */

public class ListarMunicipal extends ActionBarActivity implements AdapterView.OnItemClickListener {

    /*
* Foi implementado um evento "AdapterView.OnItemClickListener ",
* para que, quando um item da lista for clicado, ele carregar no
* formulario.
* */


        private BD database;
        private Interacoes interacoes;
        private SQLiteDatabase conexao;
        private ListView listarMunicipal;
        private ArrayAdapter<Municipal> adpMunicipal;
        private EditText pesq_municipal;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.act_listar_municipal);

            pesq_municipal = (EditText) findViewById(R.id.pesq_municipal);

            listarMunicipal = (ListView) findViewById(R.id.listarMunicipal);
            listarMunicipal.setOnItemClickListener(this); // Instanciando o evento de clicar no intem

            try{
                /*
                * Feito para analisar se a conexão com o bando de dados
                * foi realizada corretamente.
                * */
                database = new BD(this);
                conexao = database.getWritableDatabase();

                interacoes = new Interacoes(conexao);
                adpMunicipal = interacoes.ListarMunicipal(this);

                /*Vinculando ao objeto no ListView
                * para exibir os resultados encontrados.
                * */
                listarMunicipal.setAdapter(adpMunicipal);

                filtrarBairros filtrarBairros = new filtrarBairros(adpMunicipal);
                pesq_municipal.addTextChangedListener(filtrarBairros);

            }catch (SQLException ex){
                AlertDialog.Builder dlg = new AlertDialog.Builder(this);
                dlg.setMessage("Erro ! " + ex.getMessage());
                dlg.setNeutralButton("Ok", null);
                dlg.show();

            }

        }

    /*
    * Quando clicar em um bairro, irá carregar abrir uma nova act, com todas as informações.
    * */
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
             /*
        * adapterView: RETORNA REFERENCIA PARA O OBJETO ADAPTER (GETADAPTER)
        * view: RETORNA A REFERENCIA DO COMPONENTE LISTVIEW
        * I: POSIÇÃO DO ITEM SELECIONADO
        * long: ID DA LINHA* */

        /*Passando paramentros com a mesma classe it*/

        Municipal municipal = adpMunicipal.getItem(i);

        Intent it = new Intent(this, MainActivityInfo.class);
        /*Passando paramentros com a mesma classe it*/

        it.putExtra(BD.TABELA_MUNICIPAL, municipal);


        startActivityForResult(it, 0);

    }

    /*Classe para implementar uma interface, para capturar os dados que serão digitados
    * no componete texto TEXTWATCHER, no meu caso, será capturado enquando o usuário estiver
    * digitando*/

    private class filtrarBairros implements TextWatcher{

        private ArrayAdapter<Municipal> arrayAdapter;
        private filtrarBairros(ArrayAdapter<Municipal> arrayAdapter){
            this.arrayAdapter = arrayAdapter;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        /*Usarei este método, pois é o responsável por filtrar enquanto estiver digitando*/
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            arrayAdapter.getFilter().filter(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    }
} 
  • 2

    Why are you wearing setContentView(R.layout.act_listar_municipal) if your main layout is called activity_main.xml ?

No answers

Browser other questions tagged

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