Android Listview show only one item
Asked Answered
V

5

37

I have a ListView, where i changed appearence of row, but listview have size of one row, instead of fullscreen.

list_item.xml:

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

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView_news_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#0082A8" />

    <TextView
        android:id="@+id/textView_news_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

tab_news.xml:

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

<ListView
    android:id="@+id/list_news"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

 </ListView>

  </LinearLayout>

Making Adapter:

public class RSSAdapter extends ArrayAdapter<RSSitem> {
Context context; 
int layoutResourceId;    
ArrayList<RSSitem> data = null;
SimpleDateFormat sdf = new SimpleDateFormat("kk:mm ");
public RSSAdapter(Context context, int layoutResourceId, ArrayList<RSSitem> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    NewsHolder holder = null;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new NewsHolder();
        holder.txtTime = (TextView)row.findViewById(R.id.textView_news_time);
        holder.txtNews = (TextView)row.findViewById(R.id.textView_news_header);

        row.setTag(holder);
    }
    else
    {
        holder = (NewsHolder)row.getTag();
    }

    RSSitem item = data.get(position);
    holder.txtTime.setText(sdf.format(item.getPubDate()));
    holder.txtNews.setText(item.getTitle());

    return row;
}

static class NewsHolder
{
    TextView txtTime;
    TextView txtNews;
}

Items are scrolling.

Sorry for bad English. Thanks for help

Advice of @Varun didn't help

I found mistake. Thanks. Answer in "answers")

Variation answered 23/8, 2013 at 20:41 Comment(5)
your list_item has a height of fill_parent. Use wrap_content instead.Shingle
Also you have weight set up on the child views inside the LinearLayout of the list_item. You have a height of fill_parent, and you expect it not to take up the entire screen?Shingle
@Varun, Thank you, but it doesn't helpVariation
@Waqas I have to low reputation to do itVariation
upload it on a free pic-server and paste the link hereTroublous
V
99

I Found mistake. tab_news was in ScrollView of TabHost. So stupid mistake( Thanks for help.

Update:ListView must not be underScrollView in xml. If it is,the same problem occurs.

Update 2 :You may use NestedScrollView

Variation answered 24/8, 2013 at 10:24 Comment(3)
Thanks! Just a useful link about working with ScrollView and ListViewRudolfrudolfo
Ah, this saved me such a headache after not understanding my problem for 3 hours! Thanks!Cheke
Cool, it took me some time debugging all my objects to finally get here and see the easy solution.Inviolate
O
21

If you use ListView inside ScrollView, you have this issue. And my solution is following as:

1. Create a custom listview which is non scrollable

public class NonScrollListView extends ListView {

public NonScrollListView(Context context) {
    super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();    
}

}

2. Use above custom class for xml file

  <xx.xx.NonScrollListView
        android:id="@+id/lv_nonscroll_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </xx.xx.NonScrollListView>

It worked well on all OS-version for me. Hope best for you.

Outlive answered 11/8, 2017 at 7:20 Comment(2)
Nice and clever solution!Vallie
This worked for me like charm! In my own case, I have a ListView inside a RecyclerView and of course with other UI elements. Applying this solution just worked, thanks @Hai RomInglenook
S
4

In list_item.xml's parent LinearLayout, you are using android:layout_height="fill_parent" Obviously it is going to expand vertically.

OR:

use monitor tool, go to hierarchy viewer, inspect the running Layout in detail. Find the View with problem, check its attributes/properties are correct, move to its parent View and check it, until you find the issue.

Sparoid answered 24/8, 2013 at 9:25 Comment(1)
doesn't help to change it(Variation
E
0

I think Listview is taking only one record from your data. Just check that have you got all the data, which you are keen to show in Listview.

OR

Please check your list_item.xml, Replace

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

instead of,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="**fill_parent**"
android:orientation="vertical" >
Exclamation answered 24/8, 2013 at 9:21 Comment(12)
I have all records in ListView. rssListView.getAdapter().getCount()=10Variation
ok then make your list_item.xml main Linear Layout height to WRAP_CONTENT, it will work.Exclamation
@pratic, Thank you, but nothing changed(Variation
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" > Remove weight parameter and make its height wrap_content.Exclamation
@pratic , same problem(Variation
last try, please remove all the weight parameter from your TextViews and Linearlayout. It will work.Exclamation
@pratic, i updated xml file, there are no weights and all heights are "wrap_content", but nothing changed(Variation
its perfect now, clean your project and run.Exclamation
@pratic, it seems that i'm unlucky, nothing changedVariation
may be, can you please send me screen shot of your screen??Exclamation
@pratic, mail? i can't do it here because of low reputationVariation
Your xml files are fine now, no problem in it, that I am for sure. Check another things. Or come on chat room.Exclamation
S
0

Changing the height dynamically gives NPE most of the times... the best solution is to create a custom list view that is non scrollable. You'll find a lot of codes for that.

Salomone answered 8/6, 2018 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.