I've been agonising for a while now about something which ought to be relatively simple, but I can't seem to get it to work. The data for each ListView is displaying correctly, and I can select and activate the onItemClickListener by using the trackball. However, I cannot scroll down or select any of the items by touch, which I know I should be able to. Any help much appreciated. My code and xml follows:
public class POITab extends TabActivity implements AdapterView.OnItemClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poitab);
ListView raillist=(ListView)findViewById(R.id.raillist);
Cursor mrailStationsCursor = db.type_query(KEY_RAIL);
setupTab(raillist, mrailStationsCursor, "Rail", R.drawable.logo);
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("touched", " yes");
}
public void setupTab (ListView list, Cursor cursor, String tabname, int drawable) {
startManagingCursor(cursor);
String[] from_all = new String[]{DbAdapter.KEY_NAME};
int[] to_all = new int[] {android.R.id.text1};
list.setAdapter(new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1, cursor, from_all, to_all));
list.setOnItemClickListener(this);
TabHost.TabSpec spec= getTabHost().newTabSpec(tabname);
spec.setContent(list.getId());
spec.setIndicator(tabname, getResources().getDrawable(drawable));
getTabHost().addTab(spec);
}
}
poitab.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="62px">
<ListView android:id="#+id/raillist"
android:choiceMode="singleChoice"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</ListView>
</FrameLayout>
</TabHost>`
I've found out what the problem was - I had a ListView declared in the layout (not shown above, perhaps I should have included the whole source) that wasn't being used, and all touch events were being handled by that. So, moral of the story, only have the ListViews that you need within a TabHost, otherwise it will cause you problems.
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 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'm trying to create a ListView that displays 2 TextViews per row.
However I keep getting a NullPointerException when opening the Activity and I have no idea why (logCat logs don't tell me which line(s) on my end create the problem).
Here's the relevant code:
The main class EventViewActivity
public class EventViewActivity extends ListActivity {
public String[] eventTitles;
#Override
public void onCreate(Bundle savedInstanceState) {
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.eventview_layout);
eventTitles = new String[]{
getResources().getString(R.string.eventtitle1),
getResources().getString(R.string.eventtitle2),
getResources().getString(R.string.eventtitle3),
getResources().getString(R.string.eventtitle4),
getResources().getString(R.string.eventtitle5)
};
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_title, eventTitles));
setListAdapter(new ArrayAdapter<String>(this, R.layout.eventview_layout, R.id.row_descr, eventTitles));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "You have clicked an item", Toast.LENGTH_SHORT).show();
}
}
layout.xml is:
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#+id/android:eventlist" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/eventview_background"/>
</LinearLayout>
Thanks!
does your eventview_layout has a ListView with id android:id="#android:id/list" ..? if not add it.. i assume its not done
a small correction..
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"; android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/eventview_background"/>
</LinearLayout>
its necessary that the contentView of ListACtivity has a listView with exact same Id..
I think the way you are approaching for ListView is not the correct.
Please have A look on this sample LINK to have complete view how create a ListView with custom adapter.
The above link has sample application created with listview and each listitem having two text fields as your requirement.
You should call the:
super.onCreate();
first!
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 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