Android:我们必须手动创建模型类吗?

时间:2013-01-08 00:26:35

标签: android model

我正在创建一个模型类,假设它被称为“历史”,它将与SQLite数据库进行交互。

我们是否总是必须手动为模型类创建getter / setter / fields?这个没有自动发电机吗?

模型类的示例:

package com.example.fileexplorermanager;


public class History {

    //private variables
    int _id;
    String _file_name;
    String _full_path;
    String _file_type;

    // Empty constructor
    public History(){

    }
    // constructor
    public History(int id, String _name, String _full_path, String _file_type){
        this._id = id;
        this._file_name = _name;
        this._full_path = _full_path;
        this._file_type = _file_type;

    }

    // getting ID
    public int getID(){
        return this._id;
    }

    // setting id
    public void setID(int id){
        this._id = id;
    }

    public String getFileName(){
        return this._file_name;
    }

    public void setFileName(String file_name){
        this._file_name = file_name;
    }

    public String getFullPath(){
        return this._full_path;
    }

    public void setFullPath(String full_path){
        this._full_path = full_path;
    }

    public String getFileType(){
        return this._file_type;
    }

    public void setFileType(String file_type){
        this._file_type = file_type;
    }

}

4 个答案:

答案 0 :(得分:5)

如果您使用Eclipse:Source - >生成Getters和Setters ......

答案 1 :(得分:5)

更新

来自Android Studio v3.0.1:

在Android Studio中,按 ALT + INSERT (或 + N for MacOS),将有以下选择(包括您的解决方案!):

  • 构造
  • 吸气剂
  • 设置器
  • Getter和Setter
  • equals()和hashCode()
  • 的toString()
  • 覆盖方法......
  • 实施方法......
  • 委派方法......
  • 超级方法调用(在覆盖方法内部时)
  • 版权
  • 应用索引API代码(在扩展 Fragment 的类中不可用。)

选择所需选项并选择方法。完成!

答案 2 :(得分:3)

如果您在Eclipse上,请右键单击源文件 - >来源 - >生成Getters和Setter ... enter image description here

答案 3 :(得分:0)

Android没有提供任何内容,但有像ORMLite这样的第三方库。