wiki
wiki

Reputation: 299

ListView doesn't listen to onClick

I'm trying to do a ListView that it can refresh when i call the function actualizarDisplay(). I've seen in the log cat a message, and i have doubts abaout that message:

10-06 12:24:02.524: I/dalvikvm(6911): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
10-06 12:24:02.524: W/dalvikvm(6911): VFY: unable to resolve virtual method 561: Landroid/content/res/TypedArray;.getType (I)I
10-06 12:24:02.524: D/dalvikvm(6911): VFY: replacing opcode 0x6e at 0x0002
10-06 12:24:02.688: D/AbsListView(6911): Get MotionRecognitionManager
10-06 12:24:02.712: D/AbsListView(6911): onVisibilityChanged() is called, visibility : 4
10-06 12:24:02.712: D/AbsListView(6911): unregisterIRListener() is called 
10-06 12:24:02.712: D/AbsListView(6911): onVisibilityChanged() is called, visibility : 0
10-06 12:24:03.055: D/AbsListView(6911): unregisterIRListener() is called

my problem is when i push the screen the event onClickListener is not reviced.

This is my main activity:

public class Rescate extends ActionBarActivity {

ArrayList<Ficha> listaFichas = new ArrayList<Ficha>();
MyCustomAdapter dataAdapter = null;

Ficha fichaAux = new Ficha();

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

    Bundle b = this.getIntent().getExtras();
    int tipo=b.getInt("lugar");

    switch(tipo) {
        case 1:
            break;

        case 2:
            break;

        case 3:
            break;

        case 4:
            fichaAux.setNumInv("1");
            fichaAux.setIden("cuadro");
            fichaAux.setUbi("sacrsitía");
            fichaAux.setNumHom(2);
            fichaAux.setMat("Plástico");
            fichaAux.setEstado(0);
            fichaAux.setPrioridad(1);
            listaFichas.add(fichaAux);

            fichaAux = new Ficha();
            fichaAux.setNumInv("2");
            fichaAux.setIden("cuadro");
            fichaAux.setUbi("sacrsitía");
            fichaAux.setNumHom(2);
            fichaAux.setMat("Plástico");
            fichaAux.setEstado(0);
            fichaAux.setPrioridad(2);
            listaFichas.add(fichaAux);

            actualizarDisplay();
            break;

         default:
            setContentView(R.layout.activity_rescate);
    }
}

this is my function actualizarDisplay which implements the lsiteners:

public void actualizarDisplay()
    {       
        dataAdapter = new MyCustomAdapter(this,R.layout.listadofichas, listaFichas);
        ListView listView = (ListView) findViewById(R.id.listaObras);

        listView.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {           
                Intent passIntent = new Intent();
                passIntent.setClass(Rescate.this, VistaFicha.class);

                Bundle bundle = new Bundle();
                bundle.putCharSequence("num",listaFichas.get(arg2).getNumInv() );
                bundle.putCharSequence("ubicacion",listaFichas.get(arg2).getUbi() );

                passIntent.putExtras(bundle);
                startActivity(passIntent);
            }
        });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() 
        {
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3)
            {
                final String[] opciones = new String[] { "Si", "No"};
                final AlertDialog.Builder dialogo = new AlertDialog.Builder(Rescate.this);
                dialogo.setCancelable(true);
                dialogo.setTitle("¿Desea establecer que la obra ha sido rescatada?");
                dialogo.setItems(opciones, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {

                        if (item == 0)
                        {
                            listaFichas.get(arg2).setEstado(1);  
                            actualizarDisplay();
                        }
                        else
                        {
                            listaFichas.get(arg2).setEstado(0);
                            actualizarDisplay();
                        }
                    }
                    });

                dialogo.setCancelable(false);
                dialogo.create();
                dialogo.show(); 


                return false;

            }
        });

        listView.setAdapter(dataAdapter);
    }

and this are my functions to handle the ListView:

public class FichaAdapter extends BaseAdapter 
    {
        private ArrayList<Ficha> fichas;

        public FichaAdapter(ArrayList<Ficha> fichas) 
        {
            this.fichas = fichas;

            //Cada vez que cambiamos los elementos debemos noficarlo
            notifyDataSetChanged();
        }

        public int getCount() 
        {
            return fichas.size();
        }


        public Object getItem(int position) 
        {
            return fichas.get(position);
        }

        public long getItemId(int position) 
        {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            FichaView view;
            if (convertView == null) //NO existe, creamos uno
                view = new FichaView(parent.getContext());
            else                    //Existe, reutilizamos
                view = (FichaView) convertView;

            view.setFicha(fichas.get(position));

            return view;
        }
    }

private class MyCustomAdapter extends ArrayAdapter<Ficha> 
    {

        private ArrayList<Ficha> fichaList;

        public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<Ficha> fichaList) 
        {
            super(context, textViewResourceId,fichaList);
            this.fichaList = new ArrayList<Ficha>();
            this.fichaList.addAll(fichaList);
        }


        private class ViewHolder 
        {
           TextView num,iden,ubi,hombres,material;
           ImageView foto;   
           RelativeLayout fondo;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder = null;

            if (convertView == null) 
            {
               LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
               convertView = vi.inflate(R.layout.listadofichas, null);

              holder = new ViewHolder();
               holder.num = (TextView) convertView.findViewById(R.id.numInvResCon);
               holder.iden = (TextView) convertView.findViewById(R.id.idenRescCont);
               holder.ubi = (TextView) convertView.findViewById(R.id.ubiResCon);
               holder.hombres = (TextView) convertView.findViewById(R.id.numHomResCon);
               holder.material = (TextView) convertView.findViewById(R.id.matRescCon);
               holder.foto = (ImageView) convertView.findViewById(R.id.imaResc);      
               holder.fondo = (RelativeLayout) convertView.findViewById(R.id.fondoRes);
               convertView.setTag(holder);
           }
           else 
           {
               holder = (ViewHolder) convertView.getTag();
           }

           Ficha ficha = fichaList.get(position);
           holder.num.setText(ficha.getNumInv());
           holder.iden.setText(ficha.getIden());
           holder.ubi.setText(ficha.getUbi());
           holder.hombres.setText(String.valueOf(ficha.getNumHom()));
           holder.material.setText(ficha.getMat());            

           if(ficha.getPrioridad()==1)
           {
               holder.num.setTextColor(Color.RED);
               holder.iden.setTextColor(Color.RED);
               holder.ubi.setTextColor(Color.RED);
               holder.hombres.setTextColor(Color.RED);
               holder.material.setTextColor(Color.RED);
           }

           if(ficha.getPrioridad()==2)
           {
               holder.num.setTextColor(Color.parseColor("#FF8000"));
               holder.iden.setTextColor(Color.parseColor("#FF8000"));
               holder.ubi.setTextColor(Color.parseColor("#FF8000"));
               holder.hombres.setTextColor(Color.parseColor("#FF8000"));
               holder.material.setTextColor(Color.parseColor("#FF8000"));
           }

           if(ficha.getPrioridad()==3)
           {
               holder.num.setTextColor(Color.GREEN);
               holder.iden.setTextColor(Color.GREEN);
               holder.ubi.setTextColor(Color.GREEN);
               holder.hombres.setTextColor(Color.GREEN);
               holder.material.setTextColor(Color.GREEN);
           }

           if(ficha.getEstado()== 1)
           {
               holder.fondo.setBackgroundColor(Color.GREEN);
               holder.num.setTextColor(Color.WHITE);
               holder.iden.setTextColor(Color.WHITE);
               holder.ubi.setTextColor(Color.WHITE);
               holder.hombres.setTextColor(Color.WHITE);
               holder.material.setTextColor(Color.WHITE);
           }


           return convertView;

        }    
    }

I think the prgoblem is when the log cat says visibility: 0 I have a warning in this line:

convertView = vi.inflate(R.layout.listadofichas, null);

i think that the problem is here.

i write the layouts:

listadofichas.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fondoRes"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:contentDescription="@string/par" >

        <ImageView
            android:id="@+id/imaResc"
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="40dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="1dp"
            android:contentDescription="@string/vacia"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/numInv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imaResc"
            android:layout_marginLeft="100dp"
            android:layout_toRightOf="@+id/imaResc"
            android:text="@string/numInvResc"
            android:textColor="#000000"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/idenResc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/numInv"
            android:layout_below="@+id/numInv"
            android:layout_marginTop="12dp"
            android:text="@string/idenResc"
            android:textColor="#000000"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/ubiResc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/idenResc"
            android:layout_below="@+id/idenResc"
            android:layout_marginTop="12dp"
            android:text="@string/ubiResc"
            android:textColor="#000000"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/numHomResc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/ubiResc"
            android:layout_below="@+id/ubiResc"
            android:layout_marginTop="12dp"
            android:text="@string/numHomResc"
            android:textColor="#000000"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/matResc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/numHomResc"
            android:layout_below="@+id/numHomResc"
            android:layout_marginTop="12dp"
            android:text="@string/matResc"
            android:textColor="#000000"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/numInvResCon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/idenResc"
            android:layout_marginLeft="84dp"
            android:layout_toRightOf="@+id/numInv"
            android:text="@string/vacia"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/idenRescCont"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/idenResc"
            android:layout_alignLeft="@+id/numInvResCon"
            android:text="@string/vacia"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/ubiResCon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/numHomResc"
            android:layout_alignLeft="@+id/idenRescCont"
            android:text="@string/vacia"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/numHomResCon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/matResc"
            android:layout_alignLeft="@+id/ubiResCon"
            android:text="@string/vacia"
            android:textSize="@dimen/letrasPequenasAumen" />

        <TextView
            android:id="@+id/matRescCon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/matResc"
            android:layout_alignBottom="@+id/matResc"
            android:layout_alignLeft="@+id/numHomResCon"
            android:text="@string/vacia"
            android:textSize="@dimen/letrasPequenasAumen" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imaResc"
            android:layout_marginLeft="23dp"
            android:layout_marginStart="23dp"
            android:layout_toEndOf="@+id/imaResc"
            android:layout_toRightOf="@+id/imaResc"
            android:contentDescription="@string/guia"
            android:src="@drawable/barralat" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/matResc"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:src="@drawable/barrahor" />

        <Spinner
            android:id="@+id/spinner2"
            style="@style/mySpinnerItemStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/spinner1"
            android:layout_below="@+id/spinner1"
            android:layout_marginTop="13dp"
            android:entries="@array/equipos" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/numInvResCon"
            android:layout_alignBottom="@+id/numInvResCon"
            android:layout_marginLeft="203dp"
            android:layout_toRightOf="@+id/numInvResCon"
            android:text="@string/equiposAsig"
            android:textColor="#000000"
            android:textSize="@dimen/letrasPequenasAumen" />

        <Spinner
            android:id="@+id/spinner1"
            style="@style/mySpinnerItemStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="13dp"
            android:layout_marginLeft="13dp"
            android:entries="@array/equipos" />

    </RelativeLayout>

and the layout of the activity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.gepv.Rescate" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" >

        <ListView
        android:id="@+id/listaObras"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="20dp"
        tools:listitem="@layout/listadofichas" >
    </ListView>

    </RelativeLayout>

</LinearLayout>

The adpater works well because i can change the color,.. but the listener doesn't work Can anyone help me?

Upvotes: 0

Views: 113

Answers (2)

B&#246; macht Blau
B&#246; macht Blau

Reputation: 13009

So here's my code with String list items instead of Ficha ones. I made 'Rescate.java' first extend AppCompatActivity and then ActionBarActivity, and it worked for both versions:

public class Rescate extends ActionBarActivity
{
ArrayList<String> listaFichas = new ArrayList<>();
MyCustomAdapter dataAdapter = null;

String fichaAux = "Hello";

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

    // setting this up with Strings to keep things simple
    listaFichas.add("Hello");
    listaFichas.add("Hi there");
    actualizarDisplay();
}

public void actualizarDisplay()
{
    dataAdapter = new MyCustomAdapter(this,R.layout.listadofichas, listaFichas);
    ListView listView = (ListView) findViewById(R.id.listaObras);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            Intent passIntent = new Intent();
            passIntent.setClass(Rescate.this, VistaFicha.class);

            startActivity(passIntent);
        }
    });

    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
    {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3)
        {
            final String[] opciones = new String[]{"Si", "No"};
            final AlertDialog.Builder dialogo = new AlertDialog.Builder(Rescate.this);
            dialogo.setCancelable(true);
            dialogo.setTitle("¿Desea establecer que la obra ha sido rescatada?");
            dialogo.setItems(opciones, new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int item)
                {

                    if (item == 0)
                    {
                        actualizarDisplay();
                        Toast.makeText(Rescate.this, "item = 0", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(Rescate.this, "item != 0", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            dialogo.setCancelable(false);
            dialogo.create();
            dialogo.show();

            return false;

        }
    });

    listView.setAdapter(dataAdapter);
}

}

'VistaFicha.java' is just a blank activity showing "Hello World".

I put the adpater in a separate file 'MyCustomAdapter.java'

public class MyCustomAdapter extends ArrayAdapter
{

    private ArrayList<String> fichaList;
    private LayoutInflater vi;

    public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<String> fichaList)
    {
        super(context, textViewResourceId,fichaList);
        this.fichaList = new ArrayList<String>();
        this.fichaList.addAll(fichaList);
        // I had to change the original code because my adapter is not an inner class:
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    private class ViewHolder
    {
        TextView num;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;

        if (convertView == null)
        {
            convertView = vi.inflate(R.layout.listadofichas, null);

            holder = new ViewHolder();
            holder.num = (TextView) convertView.findViewById(R.id.tvHallo);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        String ficha = fichaList.get(position);
        holder.num.setText(ficha);

        return convertView;
    }
}

The xml for the list rows is simply a TextView (R.id.tvHallo) inside a LinearLayout and my Rescate activity contains only the usual "Hello World" and the ListView (R.id.listaObras)

Hope this helps, and let me know if there are any questions :)

Upvotes: 1

Yannick Widmer
Yannick Widmer

Reputation: 1363

In onCreate of your activity you have a switch in which you initiate the Adapters only if the value tipo is 4, so if it is not, the listview has no adapter and will do nothing. You could either debug to see which value it gets or at least put the line

 Log.d("Rescate","tipo = "+tipo)

to see which value it has. To see if the onItemClick is called you could plug as the first line in onItemClick the line:

Log.d("Rescate","onItemClick")

And see if this appears in the Log when you click an item in the list.

I think the error might be related to the fact that ActionBarActivity is depreciated, you should use AppCompatActivity.

If it still doesnt work could you also post the layout files you use?

Upvotes: 1

Related Questions