¿Cómo poner otros elementos con vista de lista en una actividad?
Frecuentes
Visto 599 veces
0
I want to have a header containing the logo. And then a list view. But i am struggling to get both of them in a screen. Either i get only header or only Listview. Could anyone please shed some light here. Here are my files.
// Grammar_sections.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grammar_sections);
Intent i = getIntent();
module_id =i.getStringExtra("module_id");
TestAdapter mDbHelper = new TestAdapter(getBaseContext());
mDbHelper.open();
ArrayList<String> testdata = mDbHelper.getAllSections(module_id);
for(String t : testdata )
{
//Log.d("out", t.toString());
}
mDbHelper.close();
m_listview = (ListView) findViewById(R.id.list);
if(m_listview != null)
{
Log.i("check","yup");
}
else
{
Log.i("check","nope");
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,R.layout.grammar_sections , R.id.list_item, testdata);
m_listview.setAdapter(adapter);
grammar_sections.xml
// grammar_sections.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<!-- Header Starts-->
<LinearLayout android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:paddingTop="5dip"
android:paddingBottom="5dip">
<!-- Logo Start-->
<ImageView android:src="@drawable/logo_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"/>
<ImageView android:src="@drawable/logo_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dip"/>
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<TextView
android:id="@+id/list_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="16dip"
android:typeface="sans"
android:textSize="16dip"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
/>
</RelativeLayout>
3 Respuestas
0
De tu código,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.grammar_sections , R.id.list_item, testdata);
You are passing same grammar_sections.xml archivo a su ArrayAdapter. So I think its the problem, Instead make a different xml layout file which contains list items and pass this xml layout file to ArrayAdapter constructor..
Respondido 28 ago 12, 12:08
But the image is a child in Linear Layout. So it will not matter. And as a matter of fact i need it to be fill_parent for the logo to be shown properly. Actually what i am getting from this code is a list containing all the correct values. But all of them has the header right at the top of them. So i am getting 4 values and 5 headers. - Abhinav
What about the list view? Does it have to be in the same activity as the one we mention in Array Adapter? - Abhinav
No. just ListItem means.. TextView, put TextView
in different xml layout file and pass this xml file to ArrayAdapter's constructor. And ListView remain in same grammar_sections.xml file don't change it. - user370305
0
El ListView
tiene el método añadirHeaderView. As the doc says:
Add a fixed view to appear at the top of the list.
So simply create your view
and retrive it throug an inflater
. Then add it to the ListView
Respondido 28 ago 12, 12:08
Isnt there another way? Thats too much. And everybody seems to get it by normal method only. Whats the problem with mine? Any help would be really appreciated. - Abhinav
the HeaderView could be anything deriving from View. So could a LinearLayout with all your headers - cinturón negro
0
Here a sample xml code that works:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background"
android:orientation="vertical" >
<include layout="@layout/header" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@color/white"
android:choiceMode="singleChoice"
android:divider="@color/blue_vdark"
android:dividerHeight="1dp" />
</LinearLayout>
You can replace the include by a custom View
or Layout
or add one below the ListView
.
Try to compare this code with yours. Also I don't see the </RelativeLayout>
tag at the end of your xml file.
Respondido 28 ago 12, 12:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android listview or haz tu propia pregunta.
have you tried putting the header above the xml which contains the <listview> - SquiresSquire