我如何解析我试图传递给我试图从 sqlite 游标中提取的 cardviewadapter 的变量
How can i resolve the variable i was trying to pass to a cardviewadapter that i tried to extract from sqlite cursor
ide 告诉我它无法解析我放入 ProjectsCardAdapter 参数中的符号。符号是变量 inside 一个 try 块,其中包含来自 cursor
的字符串类型
我试图在 try 块之外初始化字符串数组变量ide,但意识到我需要获取游标将有多少行来初始化字符串数组。
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;
}
}
我想使用来自 SQLite 的数据测试显示一组文本的卡片视图,但适配器无法从游标中获取变量
您的问题是 projStage、projBudget、projLoc 和 clientName 在 try 块中声明,因此仅在 try 块中具有范围。
以下内容会将范围扩大到 onCreateView 方法内:-
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;
}
}
- 注意,这是原理代码。该代码尚未经过测试或 运行,因此可能包含错误。
ide 告诉我它无法解析我放入 ProjectsCardAdapter 参数中的符号。符号是变量 inside 一个 try 块,其中包含来自 cursor
的字符串类型我试图在 try 块之外初始化字符串数组变量ide,但意识到我需要获取游标将有多少行来初始化字符串数组。
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;
}
}
我想使用来自 SQLite 的数据测试显示一组文本的卡片视图,但适配器无法从游标中获取变量
您的问题是 projStage、projBudget、projLoc 和 clientName 在 try 块中声明,因此仅在 try 块中具有范围。
以下内容会将范围扩大到 onCreateView 方法内:-
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;
}
}
- 注意,这是原理代码。该代码尚未经过测试或 运行,因此可能包含错误。