ListView ls=(ListView)findViewById(**R.id.list**);
ls.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(ListAllTracks.this);
adb.setTitle("LVSelectedItemExample");
// adb.setMessage("Selected Item is = "+String.valueOf(ls.getItemIdAtPosition(position)));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
<?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="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
views:
58answers:
2
A:
It should be this:
<ListView android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
Often, if a view id doesn't appear in the resource class (e.g. R.id.????
), that's a sign that there's a problem with the XML layout.
I'm currently using the following code, and it works for me. I am not using a ListActivity. Instead, I placed a ListView control in a standard Activity. I am using a custom adapter, but you can use a standard ArrayAdapter
if you need to.
List<Unit> unitList = ... get my list of stuff ...
ListView unitListView = (ListView)findViewById(R.id.unitListView);
UnitListItemAdapter adapter = new UnitListItemAdapter(this, unitList);
unitListView.setAdapter(adapter);
bporter
2010-08-17 18:06:04
hmm I try it with that sample first but I had an error "Your content must have a Listview whose id attribute is 'android.R.id.list'"
zire
2010-08-17 18:27:08
Is there an error somewhere else in your XML layout file? If so, then none of the IDs from that file will appear in your resource (R) object. The above code works for me.
bporter
2010-08-17 18:36:01
Hi bpoter , no I do not use anywhere list as a name for a ListView :Again this is now my XML code :<?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/list" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView></LinearLayout>now I can reference but it break when I call Activity where I fill ListView :"Your content must have a Listview whose id attribute is 'android.R.id.list'
zire
2010-08-17 18:54:32
If you can reference it, then there is no longer a problem in your XML layout, but instead there is a problem in your java code.
bporter
2010-08-17 18:58:30
bpoter I am new In this forum ?? I did not set -1 ??
zire
2010-08-17 18:58:41
hmm but i java code there is no any error until calling this activity which contain a ListView I can Paste here code of my class it is bigger then allowed :)Now if I put "@android:id/list" I do not have error and I see ListView values but now when I wont to creat onItemsClick I can reference R.id.list
zire
2010-08-17 19:06:26
I resolved this error after few hours :( android this need extends ListActivity :I change to only extend Activity and : lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));USE setAdapter !!!!Resolved when I replace setListAdapter with setAdapter :))))Thanks all of you who try to help me specialy bpoter :)
zire
2010-08-17 20:24:35
A:
change
<ListView android:id="@android:id/list"
to
<ListView android:id="@+id/android:list"
extend your Activity as ListActivity
public class myActivity extends ListActivity {
then override the onListItemClick function.
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
AlertDialog.Builder adb=new AlertDialog.Builder(ListAllTracks.this);
adb.setTitle("LVSelectedItemExample");
// adb.setMessage("Selected Item is = "+String.valueOf(ls.getItemIdAtPosition(position)));
adb.setPositiveButton("Ok", null);
adb.show();
}
Jorgesys
2010-08-17 18:56:58