Good morning! I am developing an app in Android Studio where I will use a listView in a Fragment, I have achieved the normal use of listView in normal activitys but when implementing this type of code in a fragment it does not allow it, I am doing the utility of three classes for this purpose which are BlankFragment (fragment where I will put the listView), List_adapter and Input_List, through these I load the options of the list with an image, a medium text and a small one, at the moment of executing the app, stop, I know it must be because in the fragment where I execute it is not correctly implemented the insertion of the items to the list only that is the problem, I do not know what is wrong, I do not find what I should correct, I appreciate all the possible help.
public class Lista_entrada {
private int idImagen;
private String textoEncima;
private String textoDebajo;
public Lista_entrada (int idImagen, String textoEncima, String textoDebajo) {
this.idImagen = idImagen;
this.textoEncima = textoEncima;
this.textoDebajo = textoDebajo;
}
public String get_textoEncima() {
return textoEncima;
}
public String get_textoDebajo() {
return textoDebajo;
}
public int get_idImagen() {
return idImagen;
}
}
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) {
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);
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 será la asociada a la view. La entrada es del tipo del paquete/handler
* @param view View particular que contendrá los datos del paquete/handler
*/
public abstract void onEntrada (Object entrada, View view);
}
public class BlankFragment extends ListFragment {
private ListView lista;
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_blank, container, false);
ArrayList<Lista_entrada> datos = new ArrayList<Lista_entrada>();
datos.add(new Lista_entrada(R.drawable.bolsa_de_dinero, "BUHO", "Búho es el nombre común de aves de la familia Strigidae, del orden de las estrigiformes o aves rapaces nocturnas. Habitualmente designa especies que, a diferencia de las lechuzas, tienen plumas alzadas que parecen orejas."));
lista = (ListView)getActivity().findViewById(R.id.list);
lista.setAdapter(new Lista_adaptador(getContext(), R.layout.entrada, datos){
@Override
public void onEntrada(Object entrada, View view) {
if (entrada != null) {
TextView texto_superior_entrada = (TextView) view.findViewById(R.id.textView_superior);
if (texto_superior_entrada != null)
texto_superior_entrada.setText(((Lista_entrada) entrada).get_textoEncima());
TextView texto_inferior_entrada = (TextView) view.findViewById(R.id.textView_inferior);
if (texto_inferior_entrada != null)
texto_inferior_entrada.setText(((Lista_entrada) entrada).get_textoDebajo());
ImageView imagen_entrada = (ImageView) view.findViewById(R.id.imageView_imagen);
if (imagen_entrada != null)
imagen_entrada.setImageResource(((Lista_entrada) entrada).get_idImagen());
}
}
});
return v;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BlankFragment fragment= new BlankFragment();
FragmentTransaction fragmentTransaction= getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment, "fragmentHome");
fragmentTransaction.commit();
}
}