BoinQ
BoinQ

Reputation: 27

Listview in layout Android

I have a string-array, that i need to display in a layout. The code currently shows the list, but the list isn't in the layout, its make a new "screen" where the only thing you can see is the list.

Here's the code that i just can't get to show the list in the layouts listview.

import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class idchart extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.idchart);

    Resources res = getResources();

 // To get the string array associated with particular resource ID.
 String[] ids = res.getStringArray(R.array.idchart_array);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.test_list_item,ids);
 setListAdapter(adapter);
 }

}

layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_main"
android:orientation="vertical" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/logo" />


<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<RelativeLayout
    android:id="@+id/relative"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@+id/lv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>
</RelativeLayout>

</LinearLayout>

I haven't been able to find anything that helped :( so i hope you will be able to.

Upvotes: 1

Views: 736

Answers (3)

gwvatieri
gwvatieri

Reputation: 5183

If you extend ListActivity, your ListView must have a specific id:

android:id="@id/android:list"

From the doc:

ListActivity has a default layout that consists of a single, full-screen list in the 
center of the screen. However, if you desire, you can customize the screen layout by
setting your own view layout with setContentView() in onCreate(). To do this, your own
view MUST contain a ListView object with the id "@android:id/list"

Upvotes: 0

Its not blank
Its not blank

Reputation: 3095

ListView listView=(ListView)findViewById(R.id.lv);
//rest of code
listView.setAdapter(adapter);

And you can avoid extending ListActivity.

If you are using ListActivity then

ListView listView=getListView();

Upvotes: 1

slayton
slayton

Reputation: 20319

Your idchart class should be a subclass of a regular Activity instead of a ListActivity which are for only displaying lists.

Update

Replace

public class idchart extends ListActivity 

with

public class idchart extends Activity 

Upvotes: 0

Related Questions