Get方法错误地返回空值

时间:2018-10-03 11:16:00

标签: java android sqlite

我在setter中获取数据,但没有在getter中获取数据。 下面的代码是DatabaseHelper.java

  public  List<HouseModal> get_Working_Area_Structures_House(int limit, int offset,String searched_Str) {
    if(database == null){
        database = getWritableDatabase();
    }
    List<HouseModal> list_krd= new ArrayList<>();

    String selectQuery = "SELECT DISTINCT\n" +
            "\tWAS.Para_Structure_ID,\n" +
            "\tWAS.Para_Code || '   ' || CAST (WAS.Structure_No AS int) AS Structure_No,\n" +
            "\tST.Family_Count AS Families,\n" +
            "\tFM.Family_Members_Count AS Family_Members, \n" +
            "\t FM.Family_Migration AS Family_Migration\n" +
            "FROM\n" +
            "\t (\n" +
            "\t\t SELECT\n" +
            "\t\t\tP.Para_Code,\n" +
            "\t\t\tPS.Para_Structure_ID,\n" +
            "\t\t\tPS.Structure_No \n" +
            "\t\tFROM\n" +
            "\t\t\tPara_Structure AS PS\n" +
            "\t\tINNER JOIN  Working_Area_Structures AS WAS ON PS.Para_Structure_ID = WAS.Para_Structure_ID\n" +
            "\t\tINNER JOIN  Field_Station_Paras AS P ON PS.Para_ID = P.Para_ID \n" +
            "\t) AS WAS,\n" +
            "\t (\n" +
            "\tSELECT\n" +
            "\t\t\tKH.Para_Structure_ID,\n" +
            "\t\t\tCOUNT(DISTINCT KH.House_Hold_ID) AS Family_Count \n" +
            "\t\tFROM\n" +
            "\t\t\tKhandan_Register_Header AS KH\n" +
            "\t\tLEFT JOIN  Family_Visit_History AS FVH ON KH.House_Hold_ID = FVH.House_Hold_ID \n" +
            "\t\tWHERE\n" +
            "\t\t(FVH.Visit_Status = 0 OR FVH.Visit_Status IS NULL)\n" +
            "\t\t--AND ((date(FVH.Visit_Date) >= (SELECT date(Starting_Date) FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date)) AND date(FVH.Visit_Date) <= (SELECT date(Ending_Date) FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date))) OR date(FVH.Visit_Date) IS NULL) \n" +
            "\t\t--AND (KH.House_Hold_ID NOT IN (SELECT DISTINCT House_Hold_ID FROM Family_Visit_History WHERE Visit_Status = 1)) \n" +
            "\t\tGROUP BY KH.Para_Structure_ID\n" +
            "\t) AS ST , (\n" +
            "\t\t SELECT\n" +
            "\t\t\tKH.Para_Structure_ID,\n" +
            "\t\t\tCOUNT(\n" +
            "\t\t\t\tDISTINCT KD.Family_Member_ID\n" +
            "\t\t\t) AS Family_Members_Count, \n" +
            "\t\t\tfmr.Family_Migration_ID AS Family_Migration \n" +
            "\t\tFROM\n" +
            "\t\t\tKhandan_Register_Detail AS KD\n" +
            "\t\tINNER JOIN  Khandan_Register_Header AS KH ON KD.House_Hold_ID = KH.House_Hold_ID\n" +
            "\t\tLEFT JOIN  Family_Visit_History AS FVH ON KH.House_Hold_ID = FVH.House_Hold_ID \n" +
            "\t\tleft join Family_Migration_Register as fmr on KH.House_Hold_ID = fmr.House_Hold_ID \n" +
            "\t\tWHERE\n" +
            "\t\t\t(\n" +
            "\t\t\t\tFVH.Visit_Status = 0\n" +
            "\t\t\t\tOR FVH.Visit_Status IS NULL\n" +
            "\t\t\t) \n" +
            "\t\t--AND ((date(FVH.Visit_Date) >= (SELECT date(Starting_Date)FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date)) \n" +
            "\t\t--AND date(FVH.Visit_Date) <= (SELECT date(Ending_Date) FROM Working_Cycle WHERE DATE('NOW') BETWEEN date(Starting_Date)AND date(Ending_Date))) OR date(FVH.Visit_Date) IS NULL) \n" +
            "\t\tAND (KH.House_Hold_ID NOT IN (SELECT DISTINCT House_Hold_ID FROM Family_Visit_History WHERE Visit_Status = 1 )) \n" +
            "\t\tGROUP BY\n" +
            "\t\t\tKH.Para_Structure_ID \n" +
            "\t) AS FM \n" +
            "WHERE\n" +
            "\tWAS.Para_Structure_ID = ST.Para_Structure_ID \n" +
            "\n" +
            "AND WAS.Para_Structure_ID = FM.Para_Structure_ID\n" +
            "\n" +
            "AND ST.Para_Structure_ID = FM.Para_Structure_ID\n" +
            "\n";
    if (!searched_Str.trim().equals("null") && !searched_Str.trim().equals(""))
    {
        selectQuery += " And WAS.Structure_No like '%"+searched_Str+"%'";
    }
    selectQuery += " ORDER BY WAS.Para_Code, Cast(WAS.Structure_No As int) limit "+limit+" offset "+offset;

    try{
        Cursor cursor = database.rawQuery(selectQuery, null);
        if (cursor.getCount() > 0) {
            if (cursor != null && cursor.moveToFirst()) {
                do {
                    HouseModal dkrd = new HouseModal();
                    dkrd.setStructureID(cursor.getInt(0));
                    dkrd.setStructureNo(cursor.getString(1));
                    dkrd.setFamilies(cursor.getInt(2));
                    dkrd.setMembers(cursor.getInt(3));
                    dkrd.setFamilyMigrationID(cursor.getString(4));
                    list_krd.add(dkrd);
                } while (cursor.moveToNext());
            }
        }
        cursor.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }
    return list_krd;
}

我正在获取结果设置为setFamilyMigrationID,如屏幕快照1

中所示

但无法在adpater中设置的getter中获得结果

package pk.softech.sukh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import pk.softech.sukh.R;
import pk.softech.sukh.modal.HouseModal;

public class HouseAdapter extends ArrayAdapter<HouseModal> {

    private View root;

    public HouseAdapter(Context context,List<HouseModal> items) {
        super(context,R.layout.view_house_item);
    }

    class ViewHolder{
        TextView totalAfrad,totalKhandan,structureID,migratedFamilies;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;

        HouseModal item = getItem(position);
        if(view == null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.view_house_item,null);
            holder = new ViewHolder();
            holder.structureID = (TextView) view.findViewById(R.id.structureID);
            holder.totalAfrad = (TextView) view.findViewById(R.id.totalAfrad);
            holder.totalKhandan = (TextView) view.findViewById(R.id.totalKhandan);
            holder.migratedFamilies = (TextView) view.findViewById(R.id.families);

            view.setTag(holder);
        }
        else{
            holder = (ViewHolder) view.getTag();
        }

        holder.structureID.setText(String.valueOf(item.getStructureNo()));
        holder.totalKhandan.setText(String.valueOf(item.getFamilies()));
        holder.totalAfrad.setText(String.valueOf(item.getMembers()));
        holder.migratedFamilies.setText(String.valueOf(item.getFamilyMigrationID()));

        return view;
    }
}

但是我没有得到结果,如屏幕截图2

所示,将其设置在getFamilyMigrationID中

下面是模式代码

package pk.softech.sukh.modal;

public class HouseModal {

    private int structureID, families,members;
    private String migrationID;

    private String houseHoldID;
    private String structureNo ;


    public String getFamilyMigrationID() {
        return migrationID;
    }

    public void setFamilyMigrationID(String migrationID) {
        this.migrationID = migrationID;
    }

    public String getStructureNo() {
        return structureNo;
    }

    public void setStructureNo(String structureNo) {
        this.structureNo = structureNo;
    }


    public int getStructureID() {
        return structureID;
    }

    public void setStructureID(int structureID) {
        this.structureID = structureID;
    }

    public int getFamilies() {
        return families;
    }

    public void setFamilies(int families) {
        this.families = families;
    }

    public int getMembers() {
        return members;
    }

    public void setMembers(int members) {
        this.members = members;
    }

    public String getHouseHoldID() {
        return houseHoldID;
    }

    public void setHouseHoldID(String houseHoldID) {
        this.houseHoldID = houseHoldID;
    }
}

2 个答案:

答案 0 :(得分:1)

您需要使用 getColumnIndex() 这种方法来使用游标获取列索引。

使用以下代码:

HouseModal dkrd = new HouseModal();
dkrd.setStructureID(cursor.getInt(cursor.getIndex("column name here"));
dkrd.setStructureNo(cursor.getString(cursor.getIndex("column name here")));
dkrd.setFamilies(cursor.getInt(cursor.getIndex("column name here")));
dkrd.setMembers(cursor.getInt(cursor.getIndex("column name here")));
dkrd.setFamilyMigrationID(cursor.getString(cursor.getIndex("column name here")));

代替您的代码:

HouseModal dkrd = new HouseModal();
                dkrd.setStructureID(cursor.getInt(0));
                dkrd.setStructureNo(cursor.getString(1));
                dkrd.setFamilies(cursor.getInt(2));
                dkrd.setMembers(cursor.getInt(3));
                dkrd.setFamilyMigrationID(cursor.getString(4));

  

有关 getColumnIndex(字符串columnName)的更多信息:

     

返回给定列名的从零开始的索引,如果返回则返回-1。   列不存在。如果您希望该列存在,请使用   改为 getColumnIndexOrThrow(String) ,这将使错误更加清楚。

     

参数

     

columnName:目标列的名称。

答案 1 :(得分:1)

您需要将listItem设置为super()方法,尝试下面的代码将解决您的问题

public HouseAdapter(Context context,List<HouseModal> items) {
        super(context,R.layout.view_house_item,items);
    }