开发者

ArrayAdapter's Textview takes all the screen

The Textview used by ArrayAdapter is taking the whole screen and the "title" and "description" Textviews are not shown...I'd like to show them before the list, help?

Here is my code:

TextView titleText = (TextView) this.findViewById(R.id.title);
titleText.setText(sitesList.getTitle());

TextView descriptionText = (TextView) this.findViewById(R.id.description);
descriptionText.setText(sitesList.getDescription());

ArrayList<String> Categories = sitesList.getCategory();
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.main,R.id.list_item,Categories));
setContentView(lv);

My layout file is :

<?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:orientation="vertical">
<TextView android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView android:id="@+id/description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView android:id="@+id/list_item"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />
</LinearLayout>

Thanks!


You are getting this because you set your ListView in your setContentView.Instead,add it to your LinearLayout and then your ListView would appear along with both two TextViews you specified for title and description.

One more thing,i think,you should either use android.R.layout.simple_list_item_1 in adapter or you need to create custom xml for your customized TextView and use that there,then adding

<TextView android:id="@+id/list_item"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />

in your xml.

So basically your xml would be:

<?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:id="@+id/layout"
android:orientation="vertical">
<TextView android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
<TextView android:id="@+id/description"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

and your cusotmized xml would be:

custom_item.xml

<?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:orientation="vertical">
<TextView android:id="@+id/list_item"
    android:layout_width="fill_parent" 
    android:id="@+id/list_item"
    android:layout_height="wrap_content" />
</LinearLayout>

So finally your adapter string would be:

lv.setAdapter(new ArrayAdapter<String>(this,R.layout.custom_item,R.id.list_item,Categories));

and then,

LinearLayout layout=(LinearLayout)findViewById(R.id.layout);
layout.addView(lv);


try

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">


if you want "title" and "description" Textviews and than ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
   <TextView android:id="@+id/title"
      ... />
   <TextView android:id="@+id/description"
      ... />
   <ListView android:id="@+id/list_item"
      ... />
</LinearLayout>

in java code:

setContentView(R.layouts.yourxml);

ListView lv = (ListView) findViewById(...)
lv.setAdapter(...)

if you want list item with 3 TextView use costume adapter

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜