Add a custom listview in a fragment

0

I'm trying to use a custom listview in a fragment, I've already searched several pages and I've already searched in this forum, but I can not locate myself to adapt it to my own.

My problem is that in code I can not get the custom adapter to compile, the application "thunders" when the fragment is inflated, and with an added "(AppCompatActivity)" it does not "thunder" but nothing shows the listview.

Is there anything I can do? Should I try something else?

This is the code of the fragment in which I want to show the custom listview:

private ArrayList<ListaHistorial> listaCalificaciones;

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

    View view = inflater.inflate(R.layout.fragment_historial, container, false);
    listaCalificaciones = new ArrayList<>();

    listaCalificaciones.add(new ListaHistorial("Math", 10, "9/1/2017 13:45"));
    listaCalificaciones.add(new ListaHistorial("Math", 8, "7/5/2017 10:50"));
    listaCalificaciones.add(new ListaHistorial("Marh", 4, "7/7/2017 16:30"));
    listaCalificaciones.add(new ListaHistorial("Math", 5, "4/15/2017 15:00"));
    listaCalificaciones.add(new ListaHistorial("Math", 7, "3/10/2017 10:29"));

    AdaptadorHistorial adaptador = new AdaptadorHistorial(HistorialFragment.this.getActivity(), android.R.layout.simple_list_item_1, listaCalificaciones);
    //AdaptadorHistorial adaptador = new AdaptadorHistorial(, android.R.layout.simple_list_item_1, listaCalificaciones);

    ListView list1 = (ListView)view.findViewById(R.id.list1);
    list1.setAdapter(adaptador);

    return view;
}

private class AdaptadorHistorial extends ArrayAdapter<ListaHistorial>{
    AppCompatActivity appCompatActivity;

    AdaptadorHistorial(AppCompatActivity context){
        super(context, R.layout.lista_historial, listaCalificaciones);
        appCompatActivity = context;
    }

    /*public AdaptadorHistorial(FragmentActivity activity, int simple_list_item_1, ArrayList<ListaHistorial> listaCalificaciones) {
        super(activity, simple_list_item_1, listaCalificaciones);
    }*/

    public View getView(int position, View convertView, ViewGroup parent){
        LayoutInflater inflater = appCompatActivity.getLayoutInflater();
        View item = inflater.inflate(R.layout.lista_historial, null);

        TextView tv1, tv2, tv3;
        tv1 = (TextView)item.findViewById(R.id.tv1);
        tv2 = (TextView)item.findViewById(R.id.tv2);
        tv3 = (TextView)item.findViewById(R.id.tv3);

        tv1.setText(listaCalificaciones.get(position).getExamen());
        tv2.setText(listaCalificaciones.get(position).getCalificacion());
        tv3.setText(listaCalificaciones.get(position).getFecha());

        return item;
    }
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}

This is the XML with the interface of each listview object

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

<TextView
    android:id="@+id/tv2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

<TextView
    android:id="@+id/tv3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="" />

And this is the class you create to represent each object in the listview

class ListHistorial {     private String exam, date;     private int qualification;

public ListaHistorial(String examen, int calificacion, String fecha)
{
    this.examen = examen;
    this.calificacion = calificacion;
    this.fecha = fecha;
}

public String getExamen() {
    return examen;
}

public int getCalificacion(){
    return calificacion;
}

public String getFecha(){
    return fecha;
}

}

This is where it shows the error, what it does not compile:

AdaptadorHistorial adaptador = new AdaptadorHistorial(HistorialFragment.this.getActivity(), android.R.layout.simple_list_item_1, listaCalificaciones);

I loose this error when trying to run the program:

Error:(39, 40) error: constructor AdaptadorHistorial in class HistorialFragment.AdaptadorHistorial cannot be applied to given types;

required: AppCompatActivity found: FragmentActivity, int, ArrayList reason: current and formal argument lists differ in length

If I need to show something else, please tell me. In advance, thank you very much.

    
asked by Raúl Alvarez 18.10.2017 в 21:40
source

3 answers

1

I solved the problem, it was something ... insignificant, but at first I was not clear and it is something I never found in videos or tutorials, but it was something logical. The reason why my program "thundered" is because I was trying to put an int in a TextView of the custom ListView, which has a native String format, and did not do the correct assignment of text ... So here I put the as it was at the end of the AdapterHistorial class, since this is the only one that I had to modify:

private class AdaptadorHistorial extends ArrayAdapter<ListaHistorial>{
        ...
        String paraTVCalificacion = String.valueOf(listaCalificaciones.get(position).getCalificacion());
        ...
    }
}

The corrected error is found in the commented part.

    
answered by 05.04.2018 / 08:41
source
1

You are creating a custom listview, which is why you need your Adapter to inherit from BaseAdapter, I share how you should implement it:

private class AdaptadorHistorial extends BaseAdapter {

    //Propiedades
    ArrayList<ListaHistorial>  listaCalificaciones;
    Context context;
    LayoutInflater inflater;


    //Constructor
    public AdaptadorHistorial(Context context, ArrayList<ListaHistorial> listaCalificaciones) {

        this.listaCalificaciones = listaCalificaciones;
        this.context = context;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    //Base Adapter

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

    @Override
    public Object getItem(int position) {
        return position;
    }

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


    //Holder para crear View de cada item de la lista

    class Holder {
        //Propiedades
        TextView tv1, tv2, tv3;

    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {

        View rowView = inflater.inflate(R.layout.item_customlistview, null);
        Holder holder = new Holder();

        //Init item_customlistivew
        holder.tv1 =  rowView.findViewById(R.id.tv1);
        holder.tv2 =  rowView.findViewById(R.id.tv2);
        holder.tv3 =  rowView.findViewById(R.id.tv3);

        holder.tv1.setText(listaCalificaciones.get(position).getExamen());
        holder.tv2.setText("" + listaCalificaciones.get(position).getCalificacion());
        holder.tv3.setText(listaCalificaciones.get(position).getFecha());


        return rowView;
    }
}

This is the way it is implemented in your fragment

public class CustomListFragment extends Fragment {

//Propiedades
AdaptadorHistorial adaptadorHistorial;
ArrayList<ListaHistorial> listaCalificaciones;

//View
View rootView;
ListView lvCustom;


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

    rootView = inflater.inflate(R.layout.fragment_customlistview, container, false);
    lvCustom = rootView.findViewById(R.id.lvCustom);

    listaCalificaciones = new ArrayList<>();
    listaCalificaciones.add(new ListaHistorial("Math", 10, "9/1/2017 13:45"));
    listaCalificaciones.add(new ListaHistorial("Math", 8, "7/5/2017 10:50"));
    listaCalificaciones.add(new ListaHistorial("Marh", 4, "7/7/2017 16:30"));
    listaCalificaciones.add(new ListaHistorial("Math", 5, "4/15/2017 15:00"));
    listaCalificaciones.add(new ListaHistorial("Math", 7, "3/10/2017 10:29"));

    adaptadorHistorial = new AdaptadorHistorial(getActivity(), listaCalificaciones);

    lvCustom.setAdapter(adaptadorHistorial);

    return rootView;
}

}

This is how fragment starts from the activity

public class CustomListView extends AppCompatActivity {

//Propiedades
CustomListFragment customListFragment;


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


    //Init fragment
    customListFragment = (CustomListFragment) getFragmentManager().findFragmentById(R.id.fragment_customlistview);


}

}

Finally, your main layout, the fragment, and the item of your CustomListView

activity_custom_list_view.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.grupovolada.voladadev.StackOverFlow.CustomListView"
    android:orientation="vertical">

    <fragment
        android:id="@+id/fragment_customlistview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"     class="com.grupovolada.voladadev.StackOverFlow.Fragments.CustomListFragment"></fragment>

</LinearLayout>

fragment_customlistview.xml

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

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

</LinearLayout>

item_customlistview.xml

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="" />



</LinearLayout>

I hope this solves your doubt, greetings.

    
answered by 02.11.2017 в 18:22
0

I take this thread because I am doing something very similar, it is also giving me problems and I need help. In my case, the code is as follows:

-fragment_my_stanteria.xml

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

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

</LinearLayout>

-fila_lista_miestanteria.xml

<?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">

<ImageView
    android:id="@+id/book_cover_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<TextView
    android:id="@+id/book_title_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    />

</LinearLayout>

-BookItem.java

package com.example.app_tfg;

import android.graphics.drawable.Drawable;

public class BookItem {
static String title_item;
static Drawable cover_item;

public BookItem(){
    super();
}

public BookItem(String title, Drawable cover){
    super();
    this.title_item = title;
    this.cover_item = cover;
}


public String getTitle() {
    return title_item;
}

public void setTitle(String title){
    this.title_item = title;
}

public static Drawable getCover() {
    return cover_item;
}

public void setCover(Drawable cover) {
    this.cover_item = cover;
}
}

-BookAdapter.java

public class BookAdapter extends BaseAdapter {

ArrayList<BookItem> bookList;
Context context;
LayoutInflater inflater;

public BookAdapter(Context context, ArrayList<BookItem> bookList) {
    this.bookList = bookList;
    this.context = context;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int i) {
    return i;
}

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

static class BookHolder {
    ImageView cover_item;
    TextView title_item;
}

    @Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = inflater.inflate(R.layout.fila_lista_miestanteria, null);
    BookHolder bookHolder = new BookHolder();
    bookHolder.cover_item = (ImageView) v.findViewById(R.id.book_cover_item);
    bookHolder.title_item = (TextView) v.findViewById(R.id.book_title_item);

    bookHolder.title_item.setText(bookList.get(i).getTitle());
    return v;
}
}

Now, my problem comes when I want to put everything together in the Fragment "MiEstanteria". The main problem I have is that I want to fill the Listview with data from a Google Spreadsheets spreadsheet, and I do not know where to make this call.

-MiEstanteria.java

public class MiEstanteria extends ListFragment {
static String read = "si";
ListView bookList;

public MiEstanteria() {
    // Required empty public constructor
}

//Se crea el Fragment
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

//Carga layout fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.fragment_mi_estanteria,container, false);
    bookList = (ListView) view.findViewById(android.R.id.list);
    return view;
}

private void rellenar() {
    Intent intent = new Intent(getActivity(),Spreadsheets.class);
    intent.putExtra(read_only,read);
    startActivity(intent);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {

    menuInflater.inflate(R.menu.menu_main, menu);
    super.onCreateOptionsMenu(menu, menuInflater);

    MenuItem item = menu.findItem(R.id.search);
    SearchView searchView = (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            Intent intent1 = new Intent(getActivity(), GetBookInfo.class);
            startActivity(intent1);
            return false;
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.settings:
            Intent intent2 = new Intent(getActivity(), Ajustes.class);
            startActivity(intent2);
            return true;
        case R.id.about:
            Intent intent3 = new Intent(getActivity(), About.class);
            startActivity(intent3);
            return true;
        case R.id.exit:
            new AlertDialog.Builder(getActivity()).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Salir")
                    .setMessage("¿Estás seguro?").setNegativeButton(android.R.string.cancel, null).setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //finish();
                }
            }).show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
}

Could you help me? Thank you very much in advance!

    
answered by 05.11.2017 в 18:09