Search code examples
androidandroid-listfragment

ListFragment withe image and textviews, can't click on each item


I'm trying to put a image and some textview on my list fragment, the thing it's that I can but when I execute the program I can't select any item, could somebody tellme how can I fix that, I'll paste you my code:

xml with the image and the textviews:

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


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imagen"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/nombreUsuario"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/imagen" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/calle"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/fechaPedido"
        android:layout_alignBottom="@+id/imagen"
        android:layout_toEndOf="@+id/imagen" />

arrayadapter class:

   public class AdaptadorDatosListviewChofer extends ArrayAdapter<ParseObject>{
        private Context mContexto;
        private List<ParseObject> mPedido;

    public AdaptadorDatosListviewChofer(Context contexto, List<ParseObject> pedido) {
        super(contexto, R.layout.item_lista_chofer,pedido);
        mContexto=contexto;
        mPedido=pedido;
    }

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

        if(convertView == null){
            convertView = LayoutInflater.from(mContexto).inflate(R.layout.item_lista_chofer,null);
            holder = new ViewHolder();
            holder.imagen = (ImageView) convertView.findViewById(R.id.imagen);
            holder.nombre = (TextView) convertView.findViewById(R.id.nombreUsuario);
            holder.domicilio= (TextView) convertView.findViewById(R.id.calle);
            holder.fechaPedido=(TextView) convertView.findViewById(R.id.fechaPedido);

            holder.imagen.setImageResource(R.mipmap.ic_launcher);
            holder.nombre.setText("prueba");


        }




        return convertView;
    }

    private static class ViewHolder{
            ImageView imagen;
            TextView  nombre;
            TextView  domicilio;
            TextView  fechaPedido;

    }
}

fragment where i have the list:

 public class FragmentoPrincipalChofer extends ListFragment {
    private List<ParseObject> mPedido;
    private List<ParseObject> mViaje;
    private ListView mLista;
    private Runnable r;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false);
        mLista = (ListView)x.findViewById(android.R.id.list);
        return x;

    }

    @Override
    public void onResume() {
        super.onResume();


        // logica de recibir pedidos
        final Handler handler = new Handler();

      r = new Runnable() {
            public void run() {

               handler.postDelayed(r, 10000);
                //obtener pedido de taxi
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Pedido");
                query.whereEqualTo("chofer", "chofer1");
                query.addDescendingOrder("createdAt");
                query.findInBackground(new FindCallback<ParseObject>() {
                    public void done(List<ParseObject> pedido, com.parse.ParseException e) {
                        if (e == null) {
                            mPedido = pedido;
                            String[] nombreUsuarios = new String[mPedido.size()];
                            int i = 0;
                            for (ParseObject pedidos : mPedido) {

                                nombreUsuarios[i] = pedidos.getString("cliente");
                                i++;

                            }
                            AdaptadorDatosListviewChofer adaptador = new AdaptadorDatosListviewChofer(getListView().getContext(),mPedido);
                           setListAdapter(adaptador);




                        } else {

                        }
                    }
                });
            }
        };
        r.run();



        ClickPedido();



    }


    public void ClickPedido(){

        mLista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                //verificar si aun existe el pedido en la base de datos
                final String clienteSeleccionado = (String) mLista.getItemAtPosition(position);
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Pedido");
                query.whereEqualTo("usuario",clienteSeleccionado );
                query.findInBackground(new FindCallback<ParseObject>() {
                    public void done(List<ParseObject> pedido, com.parse.ParseException e) {
                        if (e == null) {
                            //guardar viaje tomado por chofer
                            ParseObject Viaje = new ParseObject("Viaje");
                            Viaje.put("chofer", "chofer1");
                            Viaje.put("cliente", clienteSeleccionado);
                            Viaje.saveInBackground();


                            //borrar pedido xque paso a viaje


                            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Pedido");
                            query.whereEqualTo("cliente", clienteSeleccionado);
                            query.getFirstInBackground(new GetCallback<ParseObject>() {
                                @Override
                                public void done(ParseObject pedido, ParseException e) {
                                    try {
                                        pedido.delete();
                                        pedido.saveInBackground();
                                    } catch (ParseException ex) {
                                        ex.printStackTrace();
                                    }
                                }
                            });

                        } else {
                            //debo cambiarlo por un dialog
                            Toast.makeText(getContext(),"el pedido ya fue tomado",Toast.LENGTH_LONG).show();
                        }
                    }
                });

            }
        });


    }

}

xml of that fragment:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="420dp"
            android:id="@android:id/list"
            android:layout_weight="0.82"
            android:layout_marginTop="60dp" />
    </LinearLayout>

Solution

  • the problem is caused by your ImageButton try to add to your XML file this :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical" android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:weightSum="1"
       android:descendantFocusability="blocksDescendants" > //Defines the relationship between the ViewGroup and its descendants
       <ImageButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/imagen"
           android:layout_alignParentTop="true"
           android:layout_alignParentStart="true"
           android:src="@mipmap/ic_launcher"
           android:focusable="false" // set the focusability to false
           android:focusableInTouchMode="false" // also here /> ...