Igor
Igor

Reputation: 21

ListView in Android does not populate

ALL, I have a little problem. I am trying to create a following layout for my application:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView 
    android:id="@+id/output" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_marginRight="40dp"
    android:layout_marginLeft="40dp"
    android:layout_marginBottom="12dp"
    android:layout_marginTop="50dp"
    android:scaleType="center"
    android:gravity="center_horizontal"
    layout_margin="1dp" 
    android:textSize="18sp" 
    android:text=" " 
     >
    </TextView>
    <ImageView
      android:id="@+id/next"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentRight="true"
      android:layout_marginRight="50dp"
      android:layout_marginTop="340dp"
      android:src="@drawable/next"
    />
    <ImageView
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/next"
        android:layout_marginRight="20dp"
        android:layout_marginTop="340dp"
        android:src="@drawable/save"
    />
    <ListView
        android:id="@+id/filenames"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:drawSelectorOnTop="false"
        android:layout_below="@id/output"
        android:layout_above="@+id/save"
    />
</RelativeLayout>

Then in my source I use following:

    test = new ArrayList<String>();
    test.add( "a" );
    test.add( "b" );
    test.add( "c" );
    lv = (ListView) findViewById( R.id.filenames );
    adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, test );
    lv.setAdapter( adapter );
    lv.setTextFilterEnabled( true );
    lv.setOnItemClickListener( new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            m_fileclicked = (String) lv.getItemAtPosition( position );
        }
    });

The problem is that I can see the buttons and the text, but the list view is black (i.e. empty).

The code is using the Acivity, not ListActivity.

I looked around - there are at least 3 examples here on how to populate the list view when using the activity, but they all use some "crazy" layouts.

I just need to display a simple strings.

Can someone please help?

Thank you.

Upvotes: 2

Views: 1285

Answers (4)

Ron
Ron

Reputation: 24233

Your TextView fills the parent's height and your listview is placed below this textview. I think that is the problem. Your @id/output text view should layout_height="wrap_content".

<TextView 
android:id="@+id/output" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"

Update

Set the listview's background to red so you can clearly distinguish it(just test.) Please REMOVE layout_marginTop from the <ImageView>s and use layout_alignParentBottom="true" instead.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView 
    android:id="@+id/output" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" // should wrap content
    ......./>

    <ImageView
        android:id="@+id/next" 
        .......
        .......  // REMOVE MARGIN TOP
        android:layout_alignParentBottom="true" //align parent bottom
    />
    <ImageView
        android:id="@+id/save" 
        .......
        .......  // REMOVE MARGIN TOP
        android:layout_alignParentBottom="true"  //align parent bottom
    />
    <ListView
        android:id="@+id/filenames"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        .......... />
</RelativeLayout>

Upvotes: 0

Horrorgoogle
Horrorgoogle

Reputation: 7868

First you check your button id output is that next?

    android:layout_below="@id/output"
    android:layout_above="@+id/save"

another thing m_fileclicked where you declared it. Is that string or not. please correct that.

You can try this simple example:

public class ListExample extends Activity
{
 private ListView list;
 private String countries[]={"Nepal","India","China","Norway","germany"};
 @Override
 public void onCreate(Bundle icicle)
    {
   super.onCreate(icicle);
   setContentView(R.layout.main);
   list=(ListView)findViewById(R.id.ListView01);
   list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,    countries));
  list.setTextFilterEnabled(true);
  list.setOnItemClickListener(new OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
      AlertDialog.Builder builder=new AlertDialog.Builder(ListExample.this);
      builder.setTitle(" List Item Demo");
      builder.setMessage("Your Selected Item is = "+list.getItemAtPosition(Position));
      builder.setPositiveButton("Ok", null);
    builder.show();
         }
      });
    }
 }

you xml file only you can listview add and which has name ListView01.

Upvotes: 1

Balaji.K
Balaji.K

Reputation: 4829

I think you don't have clear knowledge on RelativeLayout attributes.This layout provides the designer to design the layout as they need. This layout provides such kind of features for developers. Follow these links

http://android-pro.blogspot.com/2010/03/relative-layout.html

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

http://developer.android.com/guide/topics/ui/layout-objects.html

Upvotes: 0

Ricky
Ricky

Reputation: 7879

You code looks OK to me.

I'd personally simplify your layout first (purely as a test) in case your ListView is being hidden or not expanding correctly in the RelativeLayout.

I'd place the ListView in a simple LinearLayout as a test and just have your ListView stretch height and width with nothing else in the layout:

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

If the ListView then displays correctly, you know the issue is with the RelativeLayout code as opposed to the binding of the adapter.

I guess you're binding in the onCreate() method too?

Upvotes: 1

Related Questions