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

Asked

Viewed 37 times

4

I am new on android and do not know how to do it. Follow a small piece of my code. I will check if there are no records if the list is not empty. If it is empty I need it to render another page of 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; 
}

1 answer

3


Try it this way:

 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 one layout and show as 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);
    }
}
  • Hummm That’s all?...

  • I will try yes friend!

  • this data comes from a webservice and check if the list is empty

  • I don’t know if it’s going to work this way......

  • Wait.... I’m compiling here

  • I got!!!!!!!!!!!!!

  • 1

    Just get the references if there’s anything on the list

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.