0

i want to put listview into a layout and i want to use just Activity class , not listActivity, i saw these two questions

First

Second

and i did the exactly what they say, but i still have nothing apprear in the listview

Java code

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FoodsMenu extends Activity {

    String foods[] = { "Betza" };
    ListView lvFoods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foodsmenu);
        initialize();
    }

    private void initialize() {
        // TODO Auto-generated method stub
        lvFoods = (ListView) findViewById(R.id.lvFoods);
        lvFoods.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, foods));


    }

}

layout code foodsmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/lvFoods"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#333333"
        android:fillViewport="true" >
    </ListView>

    <LinearLayout
        android:layout_width="40dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#ffffff"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="A"
            android:textColor="#025f7c"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/tvB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="B"
            android:textColor="#025f7c"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/tvC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="C"
            android:textColor="#025f7c"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/tvD"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="D"
            android:textColor="#025f7c"
            android:textSize="13dp" />

    </LinearLayout>

</LinearLayout>

what is the wrong with my works please?

Community
  • 1
  • 1
user user
  • 2,122
  • 6
  • 19
  • 24
  • which is your target os version. because same code is working for me. i tried with the code you posted only without changing any thing and its showing the list. – Raj Jan 21 '13 at 11:15
  • i tested that on samsung galaxy tab 7''. its working – Raj Jan 21 '13 at 11:17
  • this confuce me more, it works with you and i have trying to solve the problem for 2 days, what is the wrong please? i will try it in all my emulators – user user Jan 21 '13 at 11:19

2 Answers2

1

enter image description here

check the attached image. This is the screen shot of the samsung galxy tab with your code only and its working.

Below is the complete code i tested.

import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView;

public class TestSamplesActivity extends Activity {
 String foods[] = { "Betza", "Betza1", "Betza2", "Betza3", "Betza4" };
    ListView lvFoods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.foodsmenu);
        initialize();
    }

    private void initialize() {
        // TODO Auto-generated method stub
        lvFoods = (ListView) findViewById(R.id.lvFoods);
        lvFoods.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, foods));


    }
}

foodsmenu.xml.

  <?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@+id/lvFoods"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#333333"
    android:fillViewport="true" >
</ListView>

<LinearLayout
    android:layout_width="40dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="A"
        android:textColor="#025f7c"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/tvB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="B"
        android:textColor="#025f7c"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/tvC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="C"
        android:textColor="#025f7c"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/tvD"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="D"
        android:textColor="#025f7c"
        android:textSize="13dp" />

</LinearLayout>

</LinearLayout>

manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".TestSamplesActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

compare with your code if anything is missed.

Raj
  • 1,843
  • 2
  • 21
  • 47
0
  • Replace width="match_parent" of listview and width="40dp" of linear to 0dp
  • Adjust your weights accordingly like 0.8 for list and 0.2 for linear etc
Pankaj
  • 1,242
  • 1
  • 9
  • 21