来自数据库android的getter和setter

时间:2019-03-14 13:18:46

标签: android android-sqlite getter-setter

我正在尝试从数据库表中获取完整的行,并在其中发送ID并接收完整的详细信息 这是我的数据库:

  public ArrayList<Item> GetProjectToMoreDetails(String id) {
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase ();
    int ID = Integer.parseInt (id);
    Cursor cursor =  sqLiteDatabase.rawQuery ("select * from " + Projects_TABLE + " where " + PRO_ID + " = " + ID , null);

    ArrayList<Item> ProjectsArchive = new ArrayList<>();

    if (cursor != null) {
        cursor.moveToFirst ();
        for (int i = 0 ; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            Item data = new Item();
            data.setProjectName( cursor.getString( cursor.getColumnIndex( PRO_NAME )) );
            data.setPrice("$" + String.valueOf( cursor.getInt( cursor.getColumnIndex( PRO_GOAL )) ) );

            ProjectsArchive.add(data);
            cursor.moveToNext ();
        }
        cursor.close ();
    }else if (cursor == null){
        return null;
    }
    return ProjectsArchive;
}

这里是Item类中的Getter和setter方法:

public String getProjectName() {
    return ProjectName;}
public void setProjectName(String projectName) {
    this.ProjectName = projectName;}

public String getPrice() {
    return price;}

public void setPrice(String price) {
    this.price = price;}

我正试图这样称呼它,但它一直从此处返回null

Item item = new Item();
    DataBaseHelper dataBaseHelper = new DataBaseHelper( getActivity() );
    dataBaseHelper.GetProjectToMoreDetails( ProjectId );
    ProjectName.setText( item.getPrice() );
    ProjectPrice.setText( item.getPrice() );

3 个答案:

答案 0 :(得分:2)

 ArrayList<Item> itemList = dataBaseHelper.GetProjectToMoreDetails( ProjectId );
for(int i=0;i<itemList.size();i++){
    ProjectName.setText( itemList.get(i).getPrice() );
    ProjectPrice.setText( itemList.get(i).getPrice() );
}    

检查代码段。您需要这样做。

答案 1 :(得分:0)

用下面的代码替换GetProjectToMoreDetails方法。

 public ArrayList<Item> GetProjectToMoreDetails(String id) {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase ();
        int ID = Integer.parseInt (id);
        Cursor cursor =  sqLiteDatabase.rawQuery ("select * from " + Projects_TABLE + " where " + PRO_ID + " = " + ID , null);

        ArrayList<Item> ProjectsArchive = new ArrayList<>();      
        if (m_cursor != null) {
          if (m_cursor.getCount() > 0 && m_cursor.moveToFirst()) {
            do {
                Item data = new Item();
                data.setProjectName( cursor.getString( cursor.getColumnIndex( PRO_NAME )) );
                data.setPrice("$" + String.valueOf( cursor.getInt( cursor.getColumnIndex( PRO_GOAL )) ) );        
                ProjectsArchive.add(data);
             }while (m_cursor.moveToNext());
          }              
          cursor.close ();            
        }else if (cursor == null){
            return null;
        }
        return ProjectsArchive;
    }

并按照以下方式获取数据

ArrayList<Item> itemList = dataBaseHelper.GetProjectToMoreDetails( ProjectId );
for(int i=0;i<itemList.size();i++){
    ProjectName.setText( itemList.get(i).getPrice() );
    ProjectPrice.setText( itemList.get(i).getPrice() );
} 

答案 2 :(得分:0)

替换

Item item = new Item();
    DataBaseHelper dataBaseHelper = new DataBaseHelper( getActivity() );
    dataBaseHelper.GetProjectToMoreDetails( ProjectId );
    ProjectName.setText( item.getPrice() );
    ProjectPrice.setText( item.getPrice() );

使用

    DataBaseHelper dataBaseHelper = new DataBaseHelper( getActivity() );
    ArrayList<Item> ProjectsArchive=dataBaseHelper.GetProjectToMoreDetails( ProjectId );
    ProjectName.setText( ProjectsArchive.get(0).getProjectName() );
    ProjectPrice.setText( ProjectsArchive.get(0).getPrice() );

注意:-这只会打印唯一的第一项的值,要获取列表中的所有值,您应该使用recyclerview并在recycler适配器中迭代ProjectsArchive