I have a fragment that renders an xml but if there is no record I want it to render another page (xml)

4

I'm new to android and I do not know how to do that. Here's a little bit of my code. I'll check if there are no records if the list is not empty. If it is empty I need it to render the other page with no records. How can I do this?

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

        View view = inflater.inflate(R.layout.fragment_noa_cidadao2, container, false);
...
        return view; 
}
    
asked by anonymous 15.03.2017 / 15:06

1 answer

3

Try this:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
           // Verificar se ha dados ou nao
           boolean isEmpty = false;

            View view = null;

            if( isEmpty ){
                 view = inflater.inflate(R.layout.fragment_noa_cidadao2, container, false);
             }else{
                 view = inflater.inflate(R.layout.fragment_outra, container, false);
            }


            return view; 
    }

EDIT

Another way is to bring the contents of the two into a single layout and show according to the quantity of items:

Example:

Layout :

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

    <!-- SETADO A VISIBILIDADE PARA GONE -->
    <LinearLayout
        android:id="@+id/listaVazia"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </LinearLayout>


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

    </ListView>

</LinearLayout>

Java

private LinearLayout listaVazia;
private ListView listaItens;

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

       /** 
        * Inicializamos todos os componentes!
        **/

        listaItens = ListView.class.cast(findViewById(R.id.lista));
        listaVazia = LinearLayout.class.cast(findViewById(R.id.lisyVazia));

}
@Override
protected void onStart() {
    super.onStart();
    // Aqui você deverá saber se existem  itens para ser exibidos!
    if(isEmpty){
        // Se está vazia, vamos esconder a lista de Itens e exibir Lista Vazia!
        listaItens.setVisibility(View.GONE);
        listaVazia.setVisibility(View.VISIBLE);
    }else {
        listaItens.setVisibility(View.VISIBLE);
        listaVazia.setVisibility(View.GONE);
    }
}
    
15.03.2017 / 15:50