开发者

Display contents from ListView defined in regular Activity Android

开发者 https://www.devze.com 2023-04-10 11:26 出处:网络
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 a

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 s开发者_JAVA百科avedInstanceState) {
        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.

0

精彩评论

暂无评论...
验证码 换一张
取 消