Everything is done i.e adapter is created, the layout is created and data is passed but when I add ListView to the main activity, then nothing appears. I think the problem is somewhere in xml. Please help
Activity2.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.student.shopifysalespediasample.Activity2"
android:orientation="vertical"
android:id="#+id/list"
android:drawSelectorOnTop="true">
<!-- android:drawSelectorOnTop="true" shows a visual effect while clicking on listItem views-->
It shows blank screen instead of showing a sample list item in xml
tools:listitem | tools:listheader | tools:listfooter
These attributes are intended for <AdapterView> (and its subclasses like <ListView> and <RecyclerView>) and used to specify the layout that should be drawn inside that adapter as a list item, header or footer. For example, fragment_contacts_xml layout of our Contacts+ app declares <RecyclerView> and this is how it looks like before and after adding tools:listitem=”#layout/contact_item”
tools:itemCount
This attribute is intended solely for <RecyclerView> and used to specify the number of list items the layout editor should render in the layout preview.
By default Android Studio shows 10 list items for <RecyclerView>.
Therefore usually after adding tools:listitem attribute the <RecyclerView> covers the entire layout screen and you can no longer see other view elements below it. In such cases tools:itemCount attribute will help you to see the elements below the <RecyclerView>.
More you can find in hidden gems article.
You need to add ArrayAdapter or CustomAdapter along with list item layout to populate list view. If after adding adapter problem persists then please share complete code. Use this link for reference. https://www.vogella.com/tutorials/AndroidListView/article.html
Related
I have this layout (picture below). This CardView shows the information of a member. I have a list in the Room database. I want to let this layout show the list below the CardView. How can I do that?
This card here with a,b and c shows the member. Below that, I want the list shown.
1st add to library in your gradle:
compile 'com.android.support:recyclerview-v7:23.3.+'
compile 'com.android.support:cardview-v7:23.3.+'
You can create a one activity and in activity take one recyclerview.
After that you can create a Row of recyclerview and in this row take a cardview and in this card view take widgets.
Create a model class and set gatter & setter method.
Create a Adapter class to set a row and bind the List.
No in activity initialize a reyclerview and set a adapter also set some static value in list.
Follow This link for perfect example : https://www.androidhive.info/2016/05/android-working-with-card-view-and-recycler-view/
Use a LinearLayout that holds a card and then the list below that:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Your Current Card Layout Here -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</androidx.cardview.widget.CardView>
<!-- Your list of more cards -->
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" /
</LinearLayout>
Use a viewholder which uses the card view as its layout with the adapter
take a linear layout and set it orientation to vertical and after that put your card view inside this linear layout first and then put your list view or recycler view.
result will be like card view will be shown first and the list view will be shown below the card view.
My ListView is not showing the last item in the list. I am extracting all the songs inside an album and displaying these through a ListView. The ListView is displaying all the songs fine except the last song.
I verified that the adapter contains the correct count and even the ListView contains the correct count. Its just the display that is wrong. Any ideas what I am missing here?
My layout file:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
.......
<ListView
android:id="#+id/songList"
android:layout_width="420dp"
android:layout_height="220dp"
android:layout_x="84dp"
android:layout_y="260dp"
android:divider="#null"
android:scrollbars="none"
android:scrollingCache="false"
android:dividerHeight="0dp"
android:listSelector="#00000000" >
</ListView>
I have to use AbsoluteLayout because this is targeted for only one display with specific dimensions.
I strongly suspect that your AbsoluteLayout is not the correct size for the device, therefore the last item of the ListView is being cut off.
Although I know that it's not what you want, try making the height of your ListView smaller -- a height you know will not take up the whole screen -- and see if you can see all items in your ListView. From there, you can either adjust the rest of your layout to be flexible (what I recommend) or find out what the correct dimensions are for your AbsoluteLayout.
You should not use Absolute layout at all in any of the case,try to manage using Relative Layout.
If you want to display list in some part of Screen then you should consider fragment.
Can you show us what exactly you want to show in screen
I'm trying to show a list of data in an android activity. Normally anyone would do that with a simple ListView which I have used many times before. But now I'm having an application with a fixed header and footer, with the middle part (the content) scrolling underneath both the header and the footer. In the middle section I would like to add other components both above and below the list of data, but the entire part must be scrollable. I tried adding components (like a button, textview etc) to a listview but the lay-out builder in Eclipse won't let me do that.
So I started using a ScrollView where you can easily add any component you like. But I am not allowed to add a ListView to a ScrollView, which I can understand as it would create a strange effect (as both are able to scroll).
Next I wanted to use a TableLayout to dynamically add TableRows, but on multiple websites it is said to be slow and 'not the way to do it'. I also couldn't find an elegant way to add the seperator between each item. With a ListView that would all be done very easily.
The following image probably explaines at best the effect I want: http://tinyurl.com/bvkec5d
The table with the 'Table Data' header can possibly have a lot of items and thus can become very large in length. What I don't want is that the table has a fixed size and the items are scrollable within that table. I actually want the table to grow in size and the ScrollView containing the table should therefore be growing as well. I also want the infobox above the table to scroll along (as with any other components which might be added later).
What is the best way to achieve this effect?
You can use a simple vertical LinearLayout (or a RelativeLayout) that contains your static header and footer, and use a ListView between them. You can set header and footer views on the ListView to add the scrollable header and footer content. For simplicity of example here's the LinearLayout way:
<LinearLayout android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!--static header content... can be any kind of view-->
<TextView
android:layout_width="match_parent"
android:layout_weight="0"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<!--static footer content... can be any kind of view-->
<TextView
android:layout_width="match_parent"
android:layout_weight="0"
android:layout_height="wrap_content"/>
</LinearLayout>
And in code, you can say:
ListView theList = (ListView)findViewById(R.id.list);
// example of creating header and footer views from inflation or by instantiation in code
View myHeaderView = getLayoutInflater().inflate(R.layout.myHeaderLayout,theList,false);
View myFooterView = new TextView(this, some layout params);
theList.addHeaderView(myHeaderView);
theList.addFooterView(myFooterView);
ListView.addHeaderView and ListView.addFooterView should enable you to add other static views (whose content could be updated dynamically) to the top or bottom of a ListView:
public void addHeaderView (View v)
Since: API Level 1 Add a fixed view to appear at the top of the list.
If addHeaderView is called more than once, the views will appear in
the order they were added. Views added using this call can take focus
if they want.
NOTE: Call this before calling setAdapter. This is so ListView can
wrap the supplied cursor with one that will also account for header
and footer views.
Assumptions about the list (updated):
It will not contain more than 10 list elements (the list elements are defined by the xml layout below).
The height of every element is unknown, because the list element contain a LinearLayout that can have up to 20 child views (see xml below).
XML Layout of the list element:
<?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:"horizontal">
<!--
This LinearLayout is going to contain one or more
Views which will be added progammatically on runtime.
The number of children views added to it depend on the
data to be displayed, and the only assumption that can
be made is that there's will be no more than 20 child
views for one particular instance of this LinearLayout.
-->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="25dp"/>
<ImageButton
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_gravity="center|top"
android:layout_marginLeft="-25dp"/>
</LinearLayout>
Questions:
Does it make any sense to use a ListView for a layout that has such freedom
in its structure (like the one above), and still being able to make use of the
convertView as passed in to the ListView#getView(...)?
As an alternative, would it be wrong to put all the list elements in an outer
LinearLayout and put this within a ScrollView? By doing this, I wouldn't get
caching abilities of the ListView, but maybe it wouldn't be so heavy given the
assumptions about the list? (see top). (Any pointers on how to make this alternative look and feel like a ListView? I'm thinking of applying standard colors and selectors etc.)
If you know some of your 10 elemets will be the same, you could use
getItemViewType(int position)
To be sure that the convertView will match your item type convertView
I need to show a list of items, the items are read from a database, and it is possible there is no item, in this case, I just want to show a TextView saying "there is no item", I think I could implement this by using relative layout, both list and text are in center of parent, they are displayed alternatively, but is there any way better than this?
Thanks!
Adding to Aleadam , Bill Mote
You may call at any time AdapterView.setEmptyView(View v) on an AdapterView object to attach a view you would like to use as the empty view.
The snippet is as follows:
empty = (TextView)findViewById(R.id.empty1);
list = (ListView)findViewById(R.id.list1);
list.setEmptyView(empty);
Make sure you keep the ListView and TextView inside the same parent.
For detailed description please refer this
If you're using a ListActivity, that is the default behavior. If not, then you can set the ListView visibility to GONE and a TextView visibility to VISIBLE.
Aleadam is right. Here's an example XML in support of his answer:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#drawable/red"
android:gravity="center_vertical|center_horizontal"
android:textStyle="bold"
android:text="#string/error_no_groups"
/>
</LinearLayout>
The first tutorial on the Android developer website explains how to do this:
http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html
(Look under Step 4)
Snippet:
The ListView and TextView can be thought as two alternative views, only one of which will be displayed at once. ListView will be used when there are notes to be shown, while the TextView (which has a default value of "No Notes Yet!" defined as a string resource in res/values/strings.xml) will be displayed if there aren't any notes to display.
The View with the empty id is used automatically when the ListAdapter has no data for the ListView. The ListAdapter knows to look for this name by default. Alternatively, you could change the default empty view by using setEmptyView(View) on the ListView.