0

我是安卓新手。我创建了一个应用程序,然后是一个教程。我创建了一个餐厅信息页面。当我输入名称和地址和类型时,它应该显示详细信息,但它不是在模拟器中显示的

com.example.lunchlist.Restaurant@40ad8sd
com.example.lunchlist.Restaurant@40fhyu5

这是我的xml代码:

activity_lunch_list.xml

    <?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"
android:stretchColumns="1"
>
<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="@string/Namee" />
<EditText 
android:id="@+id/name"

android:ems="10" 
android:inputType="text"></EditText>

</TableRow>
<TableRow>
     <TextView
    android:text="@string/addresss" />

    <EditText
    android:id="@+id/addr"
    android:inputType="text"></EditText>
 </TableRow>
 <TableRow >
  <TextView android:text="@string/types"/>   
  <RadioGroup android:id="@+id/type">
      <RadioButton android:id="@+id/take_out"
               android:text="@string/take"   />     
      <RadioButton android:id="@+id/sit_down"
               android:text="@string/sit" /> 
      <RadioButton android:id="@+id/delivery"
               android:text="@string/deliveryy" />    
 </RadioGroup>
 </TableRow>

   <Button
    android:id="@+id/button1"
    android:text="@string/save" />
   </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>

这是我的java代码:

午餐清单.java

package com.example.lunchlist;

import java.util.List;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
//import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.view.View;
//import android.widget.EditText;
//import android.widget.RadioButton;
import android.widget.RadioGroup;
//import android.widget.Toast;


public class LunchList extends Activity {
List<Restaurant> model=new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lunch_list);
Button save=(Button)findViewById(R.id.button1);
save.setOnClickListener(onSave);
ListView list=(ListView)findViewById(R.id.restaurants);
adapter=new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
Restaurant r=new Restaurant();
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
RadioGroup types=(RadioGroup)findViewById(R.id.type);
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
r.setType("sit_down");
break;
case R.id.take_out:
r.setType("take_out");
break;
case R.id.delivery:
r.setType("delivery");
break;
}
adapter.add(r);
};
};
}

这是 Restaurant.java 页面:

package com.example.lunchlist;
public class Restaurant  {
    private String name="";
    private String address="";
    private String type="";
    public String getName() {
    return(name);
    }
    public void setName(String name) {
    this.name=name;
    }
    public String getAddress() {
    return(address);
    }
    public void setAddress(String address) {
    this.address=address;
    }

    public String getType() {
    return(type);
    }

    public void setType(String type) {
        this.type=type;
    }



}

任何帮助将不胜感激

4

1 回答 1

1

它的问题与您的适配器。您正在将Restaurant对象传递给 ,Adapter但应该将String其设置为TextView.

并且该项目不应添加到,Adapter而是必须添加到,ArrayList并且您必须通过调用将数据更改通知适配器adapter.notifyDataSetChange()

即,您的LunchList班级应如下所示

public class LunchList extends Activity
{
    List<String> model = new ArrayList<String>();
    ArrayAdapter<String> adapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lunch_list);
        Button save = (Button) findViewById(R.id.button1);
        save.setOnClickListener(onSave);
        ListView list = (ListView) findViewById(R.id.restaurants);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, model);
        list.setAdapter(adapter);
    }

    private View.OnClickListener onSave = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Restaurant r = new Restaurant();
            EditText name = (EditText) findViewById(R.id.name);
            EditText address = (EditText) findViewById(R.id.addr);
            r.setName(name.getText().toString());
            r.setAddress(address.getText().toString());
            RadioGroup types = (RadioGroup) findViewById(R.id.type);
            switch ( types.getCheckedRadioButtonId() )
            {
                case R.id.sit_down:
                    r.setType("sit_down");
                    break;
                case R.id.take_out:
                    r.setType("take_out");
                    break;
                case R.id.delivery:
                    r.setType("delivery");
                    break;
            }
            model.add(r.getType());
            adapter.notifyDataSetChanged();
        };
    };
}

注意:如果要在 ListView 中显示所有三个(名称、地址和类型),请使用自定义 ListView

带有图像和文本的 Android 自定义 ListView

使用自定义 ArrayAdapter 自定义 Android ListView 项

于 2013-04-18T06:21:29.710 回答