0

说明:- 我有一个表单,用户在其中提供他的名字和姓氏。单击包含名字和姓氏的表单上的提交按钮时,这个名字和姓氏将显示在列表的第一行,然后第二个用户将执行相同的任务,然后第二个用户的名字和姓氏将显示在第二个列表的行。

4

1 回答 1

0

您需要以以下方式编写代码以实现您的目标....

  public class Restaurant extends Activity {
  List<Restaurant> model=new ArrayList<Restaurant>();
  RestaurantAdapter adapter=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 list=(ListView)findViewById(R.id.restaurants);

    adapter=new RestaurantAdapter();
    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.types);

      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);
    }
  };

  class RestaurantAdapter extends ArrayAdapter<Restaurant> {
    RestaurantAdapter() {
      super(LunchList.this, R.layout.row, model);
    }

    public View getView(int position, View convertView,
                        ViewGroup parent) {
      View row=convertView;
      RestaurantHolder holder=null;

      if (row==null) {
        LayoutInflater inflater=getLayoutInflater();

        row=inflater.inflate(R.layout.row, parent, false);
        holder=new RestaurantHolder(row);
        row.setTag(holder);
      }
      else {
        holder=(RestaurantHolder)row.getTag();
      }

      holder.populateFrom(model.get(position));

      return(row);
    }
  }

  static class RestaurantHolder {
    private TextView name=null;
    private TextView address=null;
    private ImageView icon=null;

    RestaurantHolder(View row) {
      name=(TextView)row.findViewById(R.id.title);
      address=(TextView)row.findViewById(R.id.address);
      icon=(ImageView)row.findViewById(R.id.icon);
    }

    void populateFrom(Restaurant r) {
      name.setText(r.getName());
      address.setText(r.getAddress());

      if (r.getType().equals("sit_down")) {
        icon.setImageResource(R.drawable.ball_red);
      }
      else if (r.getType().equals("take_out")) {
        icon.setImageResource(R.drawable.ball_yellow);
      }
      else {
        icon.setImageResource(R.drawable.ball_green);
      }
    }
  }

我建议您使用以下代码:

https://github.com/commonsguy/cw-lunchlist/tree/master/05-FancyList/LunchList

开始并根据需要开发您的程序

于 2013-11-12T06:39:34.693 回答