通过Android自定义LIst视图中的用户输入(用户提供数据)动态添加图像和文本

时间:2013-11-12 06:28:07

标签: android-listview

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

1 个答案:

答案 0 :(得分: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

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