开发者

ListView is not working for android

开发者 https://www.devze.com 2023-03-27 03:22 出处:网络
So I am reading Commonsware\'s Android Programming Tutorials and I am stuck with the part where the book asks me to add a ListView. Here is my layout\'s xml

So I am reading Commonsware's Android Programming Tutorials and I am stuck with the part where the book asks me to add a ListView. Here is my layout's xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    <RelativeLayout
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">

        <TableLayout
                android:id="@+id/details"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:stretchColumns="1">
            <TableRow>
                <TextView android:text="Name:"/>
                <EditText android:id="@+id/name"/>
            </TableRow>

            <TableRow>
                <TextView android:text="Address:"/>
                <EditText android:id="@+id/address"/>
            </TableRow>
            <TableRow>
                <TextView android:text="Type: "/>
                <RadioGroup android:id="@+id/types">
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/take_out" android:text="Take-Out"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/sit_down" android:text="Sit-Down"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/delivery" android:text="Delivery"/>
                </RadioGroup>
            </TableRow>

            <Button android:id="@+id/save"
                    android:text="Save"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"/>
        </TableLayout>
        <ListView android:id="@+id/restaurants"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/details"
                />

    </RelativeLayout>
</ScrollView>

and this is my activity code

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;

import java.util.LinkedList;
import java.util.List;

public class LunchList extends Activity {

    List<Restaurant> model = new LinkedList<Restaurant>();
    ArrayAdapter<Restaurant> arrayAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button save = (Button) findViewById(R.id.save);

        save.setOnClickListener(onSave);

        ListView listView = (ListView) findViewById(R.id.restaurants);
        arrayAdapter = new ArrayAdapter<Restaurant>(this, android.R.layout.simple_list_item_1, model);
        listView.setAdapter(arrayAdapter);

    }


    private View.OnClickListener onSave = new View.OnClickListener(){

        public void onClick(View view) {
            EditText nameField = (EditText) findViewById(R.id.name);
            EditText addressField = (EditText) findViewById(R.id.address);

            Restaurant restaurant = new Restaurant();
            restaurant.setName(nameField.getText().toString());
            restaurant.setAddress(addressField.getText().toString());


            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.types);

            switch (radioGroup.getCheckedRadioButtonId()) {

                case (R.id.take_out):
                    restaurant.setType("take_out");
                    break;

                case (R.id.sit_down):
                    restaurant.setType("sit_down");
                    break;

                case (R.id.delivery):
                    restaurant.setType("delivery");
                    break;

                case (R.id.korean):
                    restaurant.setType("korean");
                    break;

                case (R.id.chinese):
                    restaurant.setType("chinese");
                    break;

                case (R.id.japanese):
                    restaurant.setType("japanese");
                    break;

                case (R.id.italian):
                    restaurant.setType("italian");
                    break;

                case (R.id.indonesian):
                    restaurant.setType("indonesian");
                    break;

            }

            arrayAdapter.add(restaurant);
            Log.i("LunchList", "Array Adapter Size: " + arrayAdapter.getCount());
        }
    };
}

I added a logging line to see whether the object is being added to the adapter or not and it looks like it is being added in there. The UI however is not showing the ListView and I do not see stuff getting added in the list.

Edit XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
    <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">

        <TableLayout
                android:id="@+id/details"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:stretchColumns="1">
            <TableRow>
                <TextView android:text="Name:"/>
                <EditText android:id="@+id/name"/>
            </TableRow>

            <TableRow>
                <TextView android:text="Address:"/>
                <EditText android:id="@+id/address"/>
            </TableRow>
            <TableRow>
                <TextView android:text="Type: "/>
                <RadioGroup android:id="@+id/types">
                    <RadioButton android:l开发者_运维技巧ayout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/take_out" android:text="Take-Out"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/sit_down" android:text="Sit-Down"/>
                    <RadioButton android:layout_height="wrap_content" android:layout_width="fill_parent"
                                 android:id="@+id/delivery" android:text="Delivery"/>
                </RadioGroup>
            </TableRow>

            <Button android:id="@+id/save"
                    android:text="Save"
                    android:layout_height="wrap_content"
                    android:layout_width="fill_parent"/>
        </TableLayout>
        <ListView android:id="@+id/restaurants"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentTop="true"
                  android:layout_above="@id/details"
                />

    </RelativeLayout>
</ScrollView>


As far as I can see, you initialise model:

List<Restaurant> model = new LinkedList<Restaurant>();

But don't put any content in it, so there is nothing for your list to show

EDIT: If you are adding content to your list dynamically, make sure that you are updating the list like this:

model.add(restaurant);
arrayAdapter.notifyDataSetChanged();

Or:

arrayAdapter.add(restaurant);
arrayAdapter.notifyDataSetChanged();

notifyDataSetChanged() lets the ListView know that the list contents have changed and it should redraw itself

EDIT: Also, you are adding the ListView above the TableLayout called details, which has its heigh and width set to fill_parent, so you may not see the ListView if the TableLayout is taking up the whole screen. Try changing the height of details to wrap_content


How exactly do you want this UI to look like? Your ListView says this:

             android:layout_alignParentTop="true"
              android:layout_above="@id/details"

So you're basically saying you want your ListView to be above details. But details is the first element, so that on is presumably on the top of the screen already!

EDIT: I wrote it in a comment, but I should amend my answer: use a LinearLayout instead (vertical orientation, of course), and give each element a weight of 1 (or adjust the weight as you wish). That way, one element can't drown out the other.

0

精彩评论

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

关注公众号