I have a piece of code
public class Radio_groupActivity extends ListActivity {
TextView selection;
String[] items={"One", "Two", "Three"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1));
}
}
Now I have the main.xml in the folder but I don't have any xml file called simple_list_item_1
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/selection"
/>
<ListView android:layout_height="fill_parent"
android:id="#+id/list"
android:layout_width="fill_parent"></ListView>
</LinearLayout>
So, what's the relationship of the listview with #id/list and that android.R.layout.simple_list_item_1?
The android. package qualifier in android.R.layout.simple_list_item_1 means you're referring to an Android framework resource rather than a resource from your app. You may not have a layout by that name, but the Android framework does.
ListActivity looks for a ListView with the id #android:id/list when using setListAdapter.
ArrayAdapter(Context context, int textViewResourceId)
android.R.layout.simple_list_item_1 is system built-in text view for list item displaying
Related
I am getting a run time exception
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'
I don't know what is wrong.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newslist);
mDbHelper.open();
fillData();
}
private void fillData() {
Bundle extras = getIntent().getExtras();
long catID = extras.getLong("cat_id");
Cursor c = mDbHelper.fetchNews(catID);
startManagingCursor(c);
String[] from = new String[] { DBHelper.KEY_TITLE };
int[] to = new int[] { R.id.newslist_text };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.text_newslist, c, from, to);
setListAdapter(notes);
}
newslist.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">
<ListView
android:id="#+id/catnewslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
text_newslist.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">
<TextView
android:text="#+id/newslist_text"
android:id="#+id/newslist_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
This will solve the error if you still want to use a ListActivity.
Remove the call to setContentView - you don't need it in a ListActivity unless you're doing something radical. The code should work without it.
Another way is, don't extend ListActivity. Just extends Activity, then you can create your list view by setContentView() and get the list view by findViewById(R.id.yourlistview).
If you extends from ListActivity, then don't use setContentView(). You need get the default list view hosted in list activity by getListView().
I had a similar issue with the error message you received, and I found it was because I was extending ListActivity instead of just Activity (that's what I get for re-purposing code from a different project ;) )
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
I face like this too. In my case (Maybe not relevant with your case, just share to others) in class mainActivity.java
yourClassName extends ListActivity(){...} change to yourClassName extends Activity(){...}
I also faced this issue, I was extending my activity class from listactivity, but the id of my list view was not defualt I changed it back to list and now its working fine.
Asim soroya
I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.
I have this code:
public class MyActivity extends ListActivity implements OnClickListener {
private ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setAdapter();
this.bindButtons();
}
private void setAdapter() {
setContentView(R.layout.siteactivity);
adapter=new ArrayAdapter<String>(this,
R.layout.siteitem,
listItems);
setListAdapter(adapter);
}
private void bindButtons() {
findViewById(R.id.buttonPrevious).setOnClickListener(this);
findViewById(R.id.buttonNext).setOnClickListener(this);
}
// ...
}
with this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/siteActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/navigation">
<Button
android:text="<="
android:textSize="12dp"
android:id="#+id/buttonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="=>"
android:textSize="12dp"
android:id="#+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/navigation"
android:layout_above="#+id/transport_selection"
/>
</RelativeLayout>
Note the android:id="#android:id/list" of the ListView. If I replace it by android:id="#+id/list", my activity force closes, because my "content must have a ListView whose id attribute is 'android.R.id.list'", but this is the expected behaviour, I think.
Now, I want to add a context menu to the items of the ListView. I tryed registerForContextMenu(findViewById(R.id.list));, but it doesn't work, because of the android:id.
Then, how can I add a context menu?
Regards,
As you are using ListActivity, you can use the following code to get list view in code:
ListView myListView=getListView ();
Use android.R.id.list instead of R.id.list.
(And by the way, the error message you wrote here turns out to have the answer)
Basically, anything defined in XML as #android:whatever is the same as android.R.whatever in the Java code. Anything that you defined in XML, which will look like #whatever, is the same as R.whatever in Java code.
Final edit: If you're using ListActivity, you are required to have a ListView with the id #android:id/list. Thus, unless you want to add another ListView to your activity, you shouldn't need #+id/list (or R.id.list).
You need to create an AlertDialog and call the dialog to show inside the listview's onItemLongClick() event. Click here for an example.
I’m tryin to make a list box that has a different image and text for each listed item. I tried to creat two setListAdapter, one for the text string to be displayed and another for the image name. It did not work, I’m assuming there is another way to do this.
My CTICTY CLASS FOR THE LIST
public class cFashions extends ListActivity {
TextView selection;
// list to fill list box wi*/
/*
String[] items={"CASUAL","DR ESSES","CAREER","OUTWEAR","FOOTWEAR",
"JEWELRY","ACCESSORIES"};
*/
String[] items={"CASUAL"};
String[] icons={"icon"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fashions);
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.label,
items));
setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, R.id.icon,
icons));
selection=(TextView)findViewById(R.id.selection);
}
}
THE XML FILE FOR THE LAYOUT OF EACH CELL IN THE LIST
<?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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:paddingLeft="2px"
android:paddingRight="2px"
android:paddingTop="2px"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
/>
</LinearLayout>
Ted
You need to use one adapter, not two, where the adapter knows how to handle both your TextView and your ImageView. Here is a free excerpt from one of my books that covers this topic in detail.
I am getting a run time exception
java.lang.RuntimeException: Your content must have a ListView whose id
attribute is 'android.R.id.list'
I don't know what is wrong.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newslist);
mDbHelper.open();
fillData();
}
private void fillData() {
Bundle extras = getIntent().getExtras();
long catID = extras.getLong("cat_id");
Cursor c = mDbHelper.fetchNews(catID);
startManagingCursor(c);
String[] from = new String[] { DBHelper.KEY_TITLE };
int[] to = new int[] { R.id.newslist_text };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.text_newslist, c, from, to);
setListAdapter(notes);
}
newslist.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">
<ListView
android:id="#+id/catnewslist"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
text_newslist.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">
<TextView
android:text="#+id/newslist_text"
android:id="#+id/newslist_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
This will solve the error if you still want to use a ListActivity.
Remove the call to setContentView - you don't need it in a ListActivity unless you're doing something radical. The code should work without it.
Another way is, don't extend ListActivity. Just extends Activity, then you can create your list view by setContentView() and get the list view by findViewById(R.id.yourlistview).
If you extends from ListActivity, then don't use setContentView(). You need get the default list view hosted in list activity by getListView().
I had a similar issue with the error message you received, and I found it was because I was extending ListActivity instead of just Activity (that's what I get for re-purposing code from a different project ;) )
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical"/>
I face like this too. In my case (Maybe not relevant with your case, just share to others) in class mainActivity.java
yourClassName extends ListActivity(){...} change to yourClassName extends Activity(){...}
I also faced this issue, I was extending my activity class from listactivity, but the id of my list view was not defualt I changed it back to list and now its working fine.
Asim soroya