Search code examples
android-fragmentsandroid-sqliteandroid-cardviewsqliteopenhelper

How can i resolve the variable i was trying to pass to a cardviewadapter that i tried to extract from sqlite cursor


The ide tells me it cannot resolve the symbols that ive put in the ProjectsCardAdapter parameters. the symbols are variables inside a try block that contains string type from cursor

I tried to initialize the string array variables outside the try block but realized i need to getCount how many rows the cursor will have in order to initialize the string arrays.

public class ProjectsFragment extends Fragment {

    public ProjectsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView projectsRecycler = (RecyclerView) inflater.inflate(
                R.layout.fragment_projects, container, false);

        try {
            SQLiteOpenHelper projectsDBhelper = new ProjectsDBhelper(inflater.getContext());
            SQLiteDatabase db = projectsDBhelper.getReadableDatabase();
            Cursor cursor = db.query("PROJECTS",
                    new String[]{"PROJ_STAGE", "PROJ_BUDGET", "PROJ_LOC", "CLIENT_NAME"}
                    , null
                    , null, null, null, null);

            int rowCount = cursor.getCount();
            String[] projStage = new String[rowCount];
            String[] projBudget = new String[rowCount];
            String[] projLoc = new String[rowCount];
            String[] clientName = new String[rowCount];
            int i = 0;
            Float floatBudget;

            if (cursor.moveToFirst()) {

                projStage[i] = cursor.getString(0);
                floatBudget = cursor.getFloat(1);
                projLoc[i] = cursor.getString(2);
                clientName[i] = cursor.getString(3);

                projBudget[i] = String.format("%,.2f", floatBudget.toString());

            cursor.close();
            db.close();

        } catch (SQLiteException e) {

            Toast exceptionToast = Toast.makeText(inflater.getContext(), "Database unavailable", Toast.LENGTH_SHORT);
            exceptionToast.show();

        }
        //This is the ProjectsCardAdapter that couldnt resolve the symbol
        ProjectsCardAdapter projectsCardAdapter = new ProjectsCardAdapter(projStage, projBudget, projLoc, clientName);
        projectsRecycler.setAdapter(projectsCardAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        projectsRecycler.setLayoutManager(layoutManager);
        return projectsRecycler;
    }
}

i wanted to test a cardview displaying a set of texts using data from SQLite but the adapter cant take the variables from cursor


Solution

  • Your issue is that projStage, projBudget, projLoc and clientName are being declared in the try block and therefore only have scope within the the try block.

    The following would increase the scope to be within the onCreateView method :-

    public class ProjectsFragment extends Fragment {
    
        public ProjectsFragment() {
            // Required empty public constructor
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            RecyclerView projectsRecycler = (RecyclerView) inflater.inflate(
                    R.layout.fragment_projects, container, false);
            String[] projStage;
            String[] projBudget;
            String[] projLoc;
            String[] clientName;
    
            try {
                SQLiteOpenHelper projectsDBhelper = new ProjectsDBhelper(inflater.getContext());
                SQLiteDatabase db = projectsDBhelper.getReadableDatabase();
                Cursor cursor = db.query("PROJECTS",
                        new String[]{"PROJ_STAGE", "PROJ_BUDGET", "PROJ_LOC", "CLIENT_NAME"}
                        , null
                        , null, null, null, null);
    
                int rowCount = cursor.getCount();
                projStage = new String[rowCount];
                projBudget = new String[rowCount];
                projLoc = new String[rowCount];
                clientName = new String[rowCount];
                int i = 0;
                Float floatBudget;
    
                if (cursor.moveToFirst()) {
                    projStage[i] = cursor.getString(0);
                    floatBudget = cursor.getFloat(1);
                    projLoc[i] = cursor.getString(2);
                    clientName[i] = cursor.getString(3);
                    projBudget[i] = String.format("%,.2f", floatBudget.toString());
    
                cursor.close();
                db.close();
    
            } catch (SQLiteException e) {
    
                Toast exceptionToast = Toast.makeText(inflater.getContext(), "Database unavailable", Toast.LENGTH_SHORT);
                exceptionToast.show();
    
            }
            //This is the ProjectsCardAdapter that couldnt resolve the symbol
            ProjectsCardAdapter projectsCardAdapter = new ProjectsCardAdapter(projStage, projBudget, projLoc, clientName);
            projectsRecycler.setAdapter(projectsCardAdapter);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            projectsRecycler.setLayoutManager(layoutManager);
            return projectsRecycler;
        }
    }
    
    • Note, this is in-principle code. The code hasn't been tested or run and therefore may contain errors.