如何在Android Studio的自定义列表视图中显示外部sqlite数据库的数据?

时间:2018-09-05 09:12:44

标签: android database sqlite oop

我想知道这是使用/ oop&sqlite在android studio中的自定义listview上显示数据的方法吗?

在这个Java页面中,我在这里分配了它们

public static Model txn;

    public static SQLiteHelper mSQLiteHelper;
    ListView mListView;
    ArrayList<Model> mList;
    RecordListAdapter mAdapter = null;

之后,我将它们初始化

mListView =findViewById(R.id.listView);
        mList = new ArrayList<>();
        mAdapter = new RecordListAdapter(this, R.layout.row, mList);
        mListView.setAdapter(mAdapter);

,这是我试图将所有代码都展示出来的代码。我在mList.add(new Model(id, name, address, phone));这行遇到了一个问题。

try{
            SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
            Cursor cursor = db.query("Table1", new String[]{"id", "name", "address", "phone"}, null,
                    null, null, null, null);
            mList.clear();
            while(cursor.moveToNext()){
                int id = cursor.getInt(3);
                String name = cursor.getString(0);
                String address = cursor.getString(1);
                String phone = cursor.getString(2);

                mList.add(new Model(id, name, address, phone));
            }
            mAdapter.notifyDataSetChanged();
            if (mList.size() == 0) {
                Toast.makeText(this, "No record found...", Toast.LENGTH_SHORT).show();
            }

            mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
                    return false;
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }

这是红色错误提示

Model() in Model cannot be applied to:
Expected Actual
Parameters: Arguments

id(int)
name(java.lang.String)
address(java.lang.String)
phone(java.lang.String)

这是我的模特页面

public class Model {
    private int id;
    private String name;
    private String address;
    private String phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

该如何解决?预先感谢...

2 个答案:

答案 0 :(得分:0)

您没有采用4个参数的构造函数。您需要类似:-

   public Model(int id,String name,String address,String phone){
      this.id = id;
      this.name = name;
      this.address = address;
      this.phone = phone;
    }

在您的Model类中。

另外使用:-

            int id = cursor.getInt(0);
            String name = cursor.getString(1);
            String address = cursor.getString(2);
            String phone = cursor.getString(3);

即偏移量0是光标的第一列,即 id 3 电话

尽管最好根据列名获取偏移量。例如(最好在表和列的名称中使用常量,而不是在各处使用硬编码)。

            int id = cursor.getInt(csr.getColumnIndex("id"));

如果这不能解决问题,请编辑您的问题以包括堆栈跟踪。

答案 1 :(得分:0)

我认为您缺少创建构造函数的方法:

模型

public class Model {
    private int id;
    private String name;
    private String address;
    private String phone;

    public Model(int id,String name,String address,String phone){
      this.id = id;
      this.name = name;
      this.address = address;
      this.phone = phone;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

我认为这应该可以解决该错误。