EDIT-现在,我了解到我应该对toString方法使用Override以实际获取正确的文本,但是,在这种情况下,我仍然对如何实现它感到困惑。有人有主意吗?
我有一个活动中的微调器,用于显示构建器的名称。这些名称是从SQLite数据库中的表中检索的。按下按钮后,微调器的选定选项随后存储在另一个表中,该表稍后将用于设置TextView的文本。
问题是,当从新表中检索名称时,它不能正确显示名称。
我尝试创建一个新表以从中检索数据,但是得到的结果相同。
将列出的构建器从第一张表加载到微调器中的方法
public void loadListedBuilders() {
String sql = "SELECT * FROM buildersTable ORDER BY name ASC"; //fetching all values from the database called "builders"
Cursor cursor = SQLiteBuilderDatabase.rawQuery(sql, null); //cursor holds all the data in the builders table
if (cursor.moveToFirst()) { //if the cursor can move to the first record, it has data and it can be fetched
do {
builderList.add(new Builder(
cursor.getInt(0), //ID
cursor.getString(1) //Name
));
} while (cursor.moveToNext()); //run the loop until the cursor has exhausted all the records
BuilderSpinnerAdapter adapter = new BuilderSpinnerAdapter(this, R.layout.newproject_builderspinner_layout, builderList, SQLiteBuilderDatabase);
editProjectBuilder.setAdapter(adapter);
}
}
将微调器中选定的构建器添加到第二个表的方法
String sql = "INSERT INTO activeProjectsTable(name, builderName, projectRate)" +
"VALUES (?, ?, ?)";
SQLiteBuilderDatabase.execSQL(sql, new String[]{projectName, projectBuilder, projectRate});
从第二个表中检索数据的方法
private void loadDailyData() {
String sql = "SELECT * FROM activeProjectsTable WHERE ProjectId = (SELECT MAX(ProjectId) FROM activeProjectsTable)"; //fetching all values from the database called "builders", and ordering the name column in alphabetical order
Cursor cursor = AusWideDatabase.rawQuery(sql,null); //cursor holds all the data in the builders table
if (cursor.moveToFirst()) { //if the cursor can move to the first record, it has data and it can be fetched
do {
projectList.add(new Project(
cursor.getInt(0), //ProjectID
cursor.getString(1), //Project Name
cursor.getString(2), //Builder Name
cursor.getDouble(3), //Project Rate
cursor.getDouble(4) //Bricks Quota
));
}
while (cursor.moveToNext()); //run the loop until the cursor has exhausted all the records
DailyDataAdapter adapter = new DailyDataAdapter(this, R.layout.layout_daily_data, projectList);
listView.setAdapter(adapter);
}
}
适配器代码,使用第二个表中的数据设置TextViews的值
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(Ctx);
View view = inflater.inflate(layoutRes, null);
TextView dataProjectName = view.findViewById((R.id.textView_dailyData_ProjectName));
TextView dataBuilderName = view.findViewById(R.id.textView_dailyData_BuilderName);
TextView dataBuilderRate = view.findViewById(R.id.textView_dailyData_ProjectRate);
Project project = projectList.get(position);
dataProjectName.setText(project.getName());
dataBuilderName.setText(project.getBuilderName());
dataBuilderRate.setText(String.valueOf(project.getProjectRate()));
return view;
}
表1
String sql = "CREATE TABLE IF NOT EXISTS buildersTable (\n" +
" BuilderId INTEGER CONSTRAINT builders_pk PRIMARY KEY AUTOINCREMENT, \n" +
" name varchar(200) \n" +
");";
表2
String sql = "CREATE TABLE IF NOT EXISTS activeProjectsTable (\n" +
" ProjectId INTEGER CONSTRAINT activeProjectsTable_pk PRIMARY KEY AUTOINCREMENT,\n" +
" name varchar(200),\n" +
" builderName varchar(200),\n" +
" projectRate double, \n" +
" averageBricks double \n" +
");";
它应该做的是显示要提取的项目的字符串值,例如生成器1。但是,目前,它显示为某种值,例如com.auswide.auswideapp.Builder@9444717代替。不知道问题出在哪里或为什么会发生。