Search code examples
androidlistviewheaderedit

hide a part (text) of a header of a list view


I have my own Adapter for a listView and a header. I want to choose which columns of listview to hide; it works for list view in my adapter with textViewIdBaseDatos.setVisibility(View.GONE) but I can't do that in the header. I could do in header.xml with android:visibility="gone" in the textView that I want to hide, but I want to chose what columns want show/hide as preferences.

You can see the header and listView in the screen: capture of the screen


header.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:background="@color/fondoAccent">

    <TextView
        android:id="@+id/editTextIdBaseDatos"
       
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:maxLength="2"
        android:text="@string/idbasedatos"
        android:textColor="@color/headlineList"
        android:textSize="@dimen/testSizeHeadList"
        android:background="@color/fondoAccent2"/>

    <TextView
        android:id="@+id/editTextPriority"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:maxLength="1"
        android:text="@string/priority"
        android:textColor="@color/headlineList"
        android:textSize="@dimen/testSizeHeadList"

        android:background="@color/fondoAccent2"/>

    <TextView
        android:id="@+id/editTextTarea"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLength="7"
        android:text="@string/task"
        android:textSize="@dimen/testSizeHeadList"
        android:textColor="@color/headlineList"
        android:background="@color/fondoAccent2"/>

A part of fragment that show the header and listView:

    Adapter = new MyAdapter(getActivity(), R.id.listView, idBaseDatos, name, description, start, startime, finished, duration, priority); // enlazamos el adaptador que hemos creado en clase java myAdapter con nuestra vista
        listView.setAdapter(myAdapter);
        listView.addHeaderView(headerView); // añade la cabecera a la listView

A part of myAdapter:

public class MyAdapter extends BaseAdapter {
    // 3 variables
    private Context context;
    private int layout;
    private List<String> priority;
    private List<String> nameTask;
    private List<String> description;
    private List<String> start;
    private List<String> startime;
    private List<String> idBasedatos;
    private List<Integer> finished;
    private List<String> duration;
    // private int colorFinished = 0xFF00FF00 ;
    private  int colorFinished;


    // constructor que recoge el contexto, el layout y los nombres
    public MyAdapter(Context context, int layout,List<String> idBasedatos, List<String> nameTask, List<String> description, List<String> start, List<String> startime, List<Integer> finished , List<String> duration, List<String> priority){
        this.context = context;
        this.priority = priority;
        this.layout = layout;
        this.nameTask = nameTask;
        this.description = description;
        this.start = start; // date
        this.startime = startime; // time in 24 H format
        this.duration = duration;
        this.idBasedatos = idBasedatos;
        this.finished = finished; // Integer que hay que pasar a booleano
        colorFinished = ContextCompat.getColor(context, R.color.colorPrimarySoft);

Solution

  • you have to first inflate the header view with listView as its parent. Now you can perform any operation on this header view before adding it as header to the listView. ex:

    ListView listView = findViewById(R.id.list);
    View header = getLayoutInflater().inflate(R.layout.header, listView, false);
    header.findViewById(R.id.viewToHide).setVisibility(View.GONE);
    listView.addHeaderView(header);
    

    you can also refer to other similar answer: https://stackoverflow.com/a/25867095/2621237