views:

41

answers:

1

I've got the following main.xml:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <ListView
    android:id="@+id/listview"
    android:layout_width="315px"
    android:layout_height="379px"
    android:layout_x="2px"
    android:layout_y="50px"
    >
    </ListView>
    <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select device:"
    android:textSize="17sp"
    android:layout_x="-2px"
    android:layout_y="1px"
    >
    </TextView>
    <Button
    android:id="@+id/refresh"
    android:layout_width="109px"
    android:layout_height="wrap_content"
    android:text="Refresh"
    android:layout_x="209px"
    android:layout_y="7px"
    >
    </Button>
</AbsoluteLayout>

The main class does nothing but drawing the layout.

I've got the following class and layout for the listitems:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

package com.android.bluetoothp2p;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BTListView extends ListActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));

      ListView lv = getListView();
      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();
        }
      });
    }

Countries is just an array with a list of countries, some dummy values.

Now, how do I make the list items of the BTListView class go in the ListView (with @+id/listview) of the main class?

EDIT: Now my list works, but it isn't clickable anymore. This is the code:

ListView lv = getListView();
      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(), "lolwat", Toast.LENGTH_SHORT).show();
        }
      });
+1  A: 

You have to give the ID @android:id/list to your ListView in your layout - see http://developer.android.com/reference/android/app/ListActivity.html for details. This ID is used by the ListActivity class to find the ListView instance.

mreichelt
Thanks, adding the items to the list and then creating the main layout made the list go into the listview with the ID you mentioned.
Tim van Dalen
However, that makes the list items unclickable
Tim van Dalen
Yeah, I guess you should override the onListItemClick() method in your activity than to set a custom handler: http://developer.android.com/reference/android/app/ListActivity.html#onListItemClick%28android.widget.ListView,%20android.view.View,%20int,%20long%29
mreichelt
Mmm, how would I do that?
Tim van Dalen
Just create a new method called onListItemClick - if you press Ctrl + Space Eclipse should automatically suggest to override the super method with the appropriate arguments. Simply put your code from your current onItemClick method there. Then you can delete your setOnItemClickListener call. You don't need to set the click listener manually because the ListActivity does this for you automatically. :-) It also hides your list view and shows a special view called "@android:id/empty" if your adapter is empty and you defined a view with that ID.
mreichelt