I am trying to implement a ListView, inside a RelativeLayout. However when I call the setAdapter() function it claims that the ListView does not exist (but there are not compilation errors).
Below is the java code:
FrameLayout framecxpesq = (FrameLayout)findViewById(R.id.layoutSearch);
ViewGroup janelapesquisa = (ViewGroup)getLayoutInflater().inflate(R.layout.janela_multisearch, null);
ListView listapesquisa = (ListView)findViewById(R.id.listViewOcorrencias);
HashMap<String,String> mapaOcorrencias1 = new HashMap<String,String>();
mapaOcorrencias1.put("pagina", "pag 1");
mapaOcorrencias1.put("resultados", "3 results found");
HashMap<String,String> mapaOcorrencias2 = new HashMap<String,String>();
mapaOcorrencias2.put("pagina", "pag 7");
mapaOcorrencias2.put("resultados", "15 resultados encontrados");
HashMap<String,String> mapaOcorrencias3 = new HashMap<String,String>();
mapaOcorrencias3.put("pagina", "pag 9");
mapaOcorrencias3.put("resultados", "10 asgsdgsdfge3333");
ArrayList<HashMap<String,String>> listamapas = new ArrayList<HashMap<String,String>>();
listamapas.add(mapaOcorrencias1);
listamapas.add(mapaOcorrencias2);
listamapas.add(mapaOcorrencias3);
if(tv_pesquisa.isSelected()){
tv_pesquisa.setSelected(false);
tv_pesquisa.setImageResource(R.drawable.search_icon);
framecxpesq.removeAllViews();
}
else{
tv_pesquisa.setSelected(true);
tv_pesquisa.setImageResource(R.drawable.search_icon_sel);
framecxpesq.addView(janelapesquisa);
listapesquisa.setAdapter(new SimpleAdapter(this,listamapas,R.layout.linha_ocorrencia,new String[]{"pagina","resultados"},new int[]{R.id.linha_item,R.id.linha_subitem}));
}
Below is XML code. One XML file defines janela_multisearch, other contains the FrameLayout layoutSearch, in which the janela_multisearch is inserted.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="320dp"
android:background="@drawable/rounded_corner_dialog" >
<include
android:id="@+id/include1"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
layout="@layout/caixapesquisa" />
<TextView
android:id="@+id/limparpag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:text="@string/clean_page_selected"
android:textColor="@color/cinza_escuro"
android:textSize="20dp" />
<TextView
android:id="@+id/cancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/include1"
android:layout_marginBottom="12.5dp"
android:layout_toRightOf="@+id/include1"
android:text="@string/cancel"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/cinza_escuro" />
<ListView
android:id="@+id/listViewOcorrencias"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/limparpag"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/include1"
android:background="@color/WHITE" >
</ListView>
The problem seems that linhapesquisa is not being found... What is this error?
You need to get the ListView from its parent View, as the layout where it is defined is not set as the layout of the Activity. Only calling findViewById()
will only search in the layout of the Activity.
ViewGroup janelapesquisa = (ViewGroup)getLayoutInflater().inflate(R.layout.janela_multisearch, null);
ListView listapesquisa = (ListView) janelapesquisa.findViewById(R.id.listViewOcorrencias);
Same with the other Views that are in this Layout.