Activity having a list and other UI - android

I have an activity which has an independent ImageView and a List. Earlier, I have worked with activities which were simple list activities and the code worked fine. But now I am having an error which says
your content must have a ListView whose attribute is 'android.R.id.list'
The original code:
setArrayAdapter(ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(partsId[position])));
XML:
<ListView
android:id="#+android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
After some research, I came to the conclusion that may be I have to use a ListView and attach that attibute to my list. I did some change in the XML too. So revised code:
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(partsId[position]));
lvParts.setAdapter(adapter);
XML:
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>
I think the reason this is happening is my Activity now has an ImageView also along with the List. Can anyone help?

android:id="#+android:id/list"
Change your listview id to above...
<ListView
android:id="#+android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
</ListView>

You have to extends your class to ListActivity instead of Activity

Related

#android:id/empty shows empty view, but does not show the ListView when data exists in ListFragment

I have a ListFragment with the following layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data" />
</LinearLayout>
When the ListFragment is initially created it has no data and everything is fine. The "no data" textview is shown as expected.
But when i add content/data the ListFragment remains in the "No Data" view, and does not switch to showing the rows. If i take out the #android:id/empty portion of the XML then i can see the rows perfectly fine. This means that it is not an issue of inputting data, but an issue regarding the actual XML file (i belive).
Is there any way to remedy this?
Update:
Calling notifyDataSetChanged() when adding to the List resolved this issue!
What if you add this code to the fragment, where you set an adapter on the listview?
list.setEmptyView(findViewById(android.R.id.empty)) ?

ListView inside Fragment android

Im having trouble with implementing list view inside a fragment.
the xml code 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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/jo_logo" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
simple ImageView and afterwards the list view.
I've already tried to implement:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.simple_list_item_1, viewList);
setListAdapter(adapter);
where viewList is View[], on the onCreateView function.
seems that it's not working that way inside fragment.
thing is i need that the first view will be ImageView and second one is ListView, and all inside the Fragment.
please help, thank in advance, udi
In your layout xml change android:id="#+id/listView1" to android:id="#android:id/list" otherwise the ListActivity / ListFragment won't find your ListView.

Android: id list view

I have a list view declared in my xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#F7EFE8">
<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="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_message"/>
</LinearLayout>
But when I try to declare/use it to java, the id is not found. But when i rename the list view to something else and try to use it i can see it "R.id._" but when R.id.list I can't find it. And If I didn't use the android:list there's an error that there should be a list view whose id is android:list. Any help here?
Your ListView object id should be specified either as android:id="#android:id/list"
ListView lv = (ListView) findViewById(android.R.id.list);
or it should have some other id like android:id="#+id/sampleList"
ListView lv = (ListView) findViewById(R.id.sampleList);
Check this : ListActivity
and list
Hope this would help.
In xml layout use,
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
In Java Code:
ListView listview=(ListView)findViewById(R.id.list);// it takes id of listview from xml
if you need to use android id for listview then replace your code as
<ListView
android:id="#+android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
It may help you..
Try this: android:id="#+id/list"
Then clean project and rebuild.
In java, just call findViewById like this:
ListView lv = (ListView)findViewById(R.id.list);
Ref: http://developer.android.com/reference/android/view/View.html

Customizing ListView via XML Problem

I have a simple ListActivity that uses a ListAdapter that I'd like to customize the ListView for. Here is my onCreate, where I specify to use a View rather than using the default ListView generated by setListAdapter():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the listview, empty listview
setContentView(R.layout.main);
getListView().setEmptyView(findViewById(android.R.id.empty));
// set the list properties
getListView().setOnCreateContextMenuListener(this);
.
.
.
<code snipped>
.
.
.
setListAdapter(adapter);
}
You can see that I am setting the emptyView as well. Here is the XML file main.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">
<ListView
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdge="vertical"
android:dividerHeight="5dp" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:singleLine="false"
android:text="#string/message_empty_list" />
</LinearLayout>
My problem is that the ListView, while applying the dividerHeight correctly, is ignoring the :fadingEdge directive when it is in the XML; however, if I set the fadingEdge programatically, it works, i.e.:
getListView().setVerticalFadingEdgeEnabled(true);
Any ideas why the listview would be ignoring the fadingEdge specifically? Am I doing something incorrectly that could be leading to this behavior?
Thanks!
android:id="#id/android:list"
should be
android:id="#iandroid:id/list"
I'm not even sure why that compiles.
This is how you should be using custom xml for listactivities.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data found"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>

Showing empty view when ListView is empty

For some reason the empty view, a TextView in this case, always appears even when the ListView is not empty. I thought the ListView would automatically detect when to show the empty view.
<RelativeLayout android:id="#+id/LinearLayoutAR"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="#+id/ARListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
<ProgressBar android:id="#+id/arProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"></ProgressBar>
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
</RelativeLayout>
How can I hook up the empty view properly?
When you extend FragmentActivity or Activity and not ListActivity, you'll want to take a look at:
ListView.setEmptyView()
It should be like this:
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
Note the id attribute.
As appsthatmatter says, in the layout something like:
<ListView android:id="#+id/listView" ... />
<TextView android:id="#+id/emptyElement" ... />
and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.emptyElement));
Does also work with a GridView...
I tried all the above solutions.I came up solving the issue.Here I am posting the full solution.
The xml file:
<RelativeLayout
android:id="#+id/header_main_page_clist1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:paddingBottom="10dp"
android:background="#ffffff" >
<ListView
android:id="#+id/lv_msglist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider_color"
android:dividerHeight="1dp" />
<TextView
android:id="#+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO MESSAGES AVAILABLE!"
android:textColor="#525252"
android:textSize="19.0sp"
android:visibility="gone" />
</RelativeLayout>
The textView ("#+id/emptyElement") is the placeholder for the empty listview.
Here is the code for java page:
lvmessage=(ListView)findViewById(R.id.lv_msglist);
lvmessage.setAdapter(adapter);
lvmessage.setEmptyView(findViewById(R.id.emptyElement));
Remember to place the emptyView after binding the adapter to listview.Mine was not working for first time and after I moved the setEmptyView after the setAdapter it is now working.
Output:
I highly recommend you to use ViewStubs like this
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ViewStub
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty" />
</FrameLayout>
See the full example from Cyril Mottier
<ListView android:id="#+id/listView" ... />
<TextView android:id="#+id/empty" ... />
and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.empty));
This works clearly with FragmentActivity if you are using the support library. Tested this by building for API 17 i.e. 4.2.2 image.
Activity code, its important to extend ListActivity.
package com.example.mylistactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.example.mylistactivity.R;
// It's important to extend ListActivity rather than Activity
public class MyListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
// shows list view
String[] values = new String[] { "foo", "bar" };
// shows empty view
values = new String[] { };
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
values));
}
}
Layout xml, the id in both views are important.
<?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">
<!-- the android:id is important -->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- the android:id is important -->
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="i am empty"/>
</LinearLayout>
Just to add that you don't really need to create new IDs, something like the following will work.
In the layout:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:text="Empty"/>
Then in the activity:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setEmptyView(findViewById(android.R.id.empty));
I had this problem. I had to make my class extend ListActivity rather than Activity, and rename my list in the XML to android:id="#android:id/list"
A programmatically solution will be:
TextView textView = new TextView(context);
textView.setId(android.R.id.empty);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("No result found");
listView.setEmptyView(textView);
First check the list contains some values:
if (list.isEmpty()) {
listview.setVisibility(View.GONE);
}
If it is then OK, otherwise use:
else {
listview.setVisibility(View.VISIBLE);
}

Categories

Resources