ListView in Fragment type?

2

I have this to create a particular ListView:

The problem is that it is a Fragment , and when you run the application and click on the Fragment , it stops and goes away, nothing is shown. What's wrong?

"First_fragment.java" :

public class First_fragment extends Fragment {

    private ListView lista1;
    public final static String CLAVE_EXTRA_PASAR2 = "true";

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

        View v = inflater.inflate(R.layout.first_tab,container,false);

            ArrayList<Lista_entrada> datos = new ArrayList<>();

            datos.add(new Lista_entrada(getResources().getDrawable(R.drawable.stopwatch),"TENIS", "Iluminacion de las pistas 1 - 5"));
            datos.add(new Lista_entrada(getResources().getDrawable(R.drawable.stopwatch),"PADEL", "Iluminacion de las pistas 1 - 7"));
            datos.add(new Lista_entrada(getResources().getDrawable(R.drawable.stopwatch),"FRONTON", "Activar o desactivar iluminacion"));
            datos.add(new Lista_entrada(getResources().getDrawable(R.drawable.stopwatch),"FUTBOL", "Activar o desactivar iluminacion"));
            datos.add(new Lista_entrada(getResources().getDrawable(R.drawable.stopwatch),"CLUB SOCIAL", "Iluminacion por zonas"));

            lista1 = (ListView) getActivity().findViewById(R.id.mylist);

            //Esto es para el cuadradito que te dice lo que has elegido.

            lista1.setAdapter(new Lista_adaptador(getActivity(), R.layout.boton_main, datos, null){ // list_main donde aparecera
                @Override
                public void onEntrada(Object entrada, View view) {
                    if (entrada != null) {
                        TextView texto_superior_entrada = (TextView) view.findViewById(R.id.textView2);
                        if (texto_superior_entrada != null)
                            texto_superior_entrada.setText(((Lista_entrada) entrada).get_textoEncima());

                        TextView texto_inferior_entrada = (TextView) view.findViewById(R.id.textView3);
                        if (texto_inferior_entrada != null)
                            texto_inferior_entrada.setText(((Lista_entrada) entrada).get_textoDebajo());

                    }
                }
            });
            lista1.setOnItemClickListener(new AdapterView.OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> pariente, View view, int posicion, long id){

                    Lista_entrada elegido = (Lista_entrada) pariente.getItemAtPosition(posicion);

                    if(posicion == 0){
                        Intent intencion3 = new Intent(getActivity().getApplicationContext(), Antonio.class);
                        intencion3.putExtra(CLAVE_EXTRA_PASAR2, "true");
                        startActivity(intencion3);
                    }
                    else if(posicion == 1){
                        Intent intencion4 = new Intent(getActivity().getApplicationContext(), Antonio.class);
                        intencion4.putExtra(CLAVE_EXTRA_PASAR2, "true");
                        startActivity(intencion4);
                    }
                    else if(posicion == 2){
                        Intent intencion5 = new Intent(getActivity().getApplicationContext(), Antonio.class);
                        intencion5.putExtra(CLAVE_EXTRA_PASAR2, "true");
                        startActivity(intencion5);
                    }
                    else if(posicion == 3){
                        Intent intencion6 = new Intent(getActivity().getApplicationContext(), Antonio.class);
                        intencion6.putExtra(CLAVE_EXTRA_PASAR2, "true");
                        startActivity(intencion6);
                    }
                    else if(posicion == 4){
                        Intent intencion7 = new Intent(getActivity().getApplicationContext(), Antonio.class);
                        intencion7.putExtra(CLAVE_EXTRA_PASAR2, "true");
                        startActivity(intencion7);
                    }
                }
            });
        return v;
    }
}

"Entry_list.java" :

public class Lista_entrada {

    private String textoEncima;
    private String textoDebajo;
    private Drawable icono_deportes;

    public Lista_entrada (Drawable icono_deportes, String textoEncima, String textoDebajo) {
        super();
        this.textoEncima = textoEncima;
        this.textoDebajo = textoDebajo;
        this.icono_deportes = icono_deportes;
    }

    public String get_textoEncima() {
        return textoEncima;
    }

    public String get_textoDebajo() {
        return textoDebajo;
    }

    public Drawable get_icono_deportes(){
        return icono_deportes;
    }
}

Adapter_list :

public abstract class Lista_adaptador extends BaseAdapter {

    private ArrayList<?> entradas;
    private int R_layout_IdView;
    private Context contexto;


    public Lista_adaptador(Context contexto, int R_layout_IdView, ArrayList<?> entradas, Activity activity) {
        super();
        this.contexto = contexto;
        this.entradas = entradas;
        this.R_layout_IdView = R_layout_IdView;
    }

    @Override
    public View getView(int posicion, View view, ViewGroup pariente) {
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R_layout_IdView, null);
        }

        onEntrada (entradas.get(posicion), view);
        Lista_entrada dir = (Lista_entrada) entradas.get(posicion);
        ImageView icono_deportes = (ImageView) view.findViewById(R.id.icono_deportes);
        icono_deportes.setImageDrawable(dir.get_icono_deportes());
        return view;
    }

    @Override
    public int getCount() {
        return entradas.size();
    }

    @Override
    public Object getItem(int posicion) {
        return entradas.get(posicion);
    }

    @Override
    public long getItemId(int posicion) {
        return posicion;
    }

    /** Devuelve cada una de las entradas con cada una de las vistas a la que debe de ser asociada
     * @param entrada La entrada que sera la asociada a la view. La entrada es del tipo del paquete/handler
     * @param view View particular que contendra los datos del paquete/handler
     */
    public abstract void onEntrada (Object entrada, View view);
}

"activity_profesorado.xml" :

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


    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

boton_main.xml :

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="120dp">

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:textStyle="normal|bold"
            android:textSize="30sp"
            android:textAlignment="center" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView3"
            android:textAlignment="center" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/stopwatch"
            android:id="@+id/icono_deportes" />
    </LinearLayout>
</LinearLayout>
    
asked by Rf Mvs 27.10.2016 в 21:57
source

1 answer

0

I think the problem is this.

lista1 = (ListView) getActivity().findViewById(R.id.mylist);

You should have this:

lista1 = (ListView) **view**.findViewById(R.id.mylist);

Let me know if you need more help and if that was not the eroor. Regards! Anything you need to order:)

    
answered by 12.05.2017 в 14:58