Android studio新手 求助

weixin-13129929852 2020-03-12 02:44:19
做毕业设计的学生一枚,基于Android的课程表,要实现的一个功能是点击空白格子增加课程信息。现在的问题是:增加课程信息时点击保存,所增加的信息没有出现在指定的格子上。代码没有报错,真机调试时出现下面的:

03/10 22:04:27: Launching app
$ adb push D:\b\app\build\outputs\apk\debug\app-debug.apk /data/local/tmp/com.example.b
$ adb shell pm install -t -r "/data/local/tmp/com.example.b"
pkg: /data/local/tmp/com.example.b
Success
APK installed in 8 s 463 ms
$ adb shell am start -n "com.example.b/com.example.b.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 22844 on device oppo-oppo_a57-32142c6c
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/System: ClassLoader referenced unknown path: /data/app/com.example.b-1/lib/arm64
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/Adreno: QUALCOMM build : 31f65f7, Ia3ef73d9
Build Date : 12/03/16
OpenGL ES Shader Compiler Version: XE031.08.00.00
Local Branch :
Remote Branch :
Remote Branch :
Reconstruct Branch :
I/OpenGLRenderer: Initialized EGL, version 1.4
Application terminated.
请给位前辈帮忙看一下,是什么问题?源码有点长,如果上面的不足以判断我再贴出来
...全文
511 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin-13129929852 2020-03-21
  • 打赏
  • 举报
回复
引用 4 楼 忘不川 的回复:
添加成功后返回的时候 这里要取出数据 重写设置数据的啊
//返回时刷新界面
@Override
protected void onResume(){
super.onResume();
setTextContent()
}
建议使用startActivityForResult()

喔~,谢谢,我试试
忘不川 2020-03-18
  • 打赏
  • 举报
回复
添加成功后返回的时候 这里要取出数据 重写设置数据的啊 //返回时刷新界面 @Override protected void onResume(){ super.onResume(); setTextContent() } 建议使用startActivityForResult()
weixin-13129929852 2020-03-18
  • 打赏
  • 举报
回复
各位路过的前辈,帮忙看看,万分感激!!!
weixin-13129929852 2020-03-13
  • 打赏
  • 举报
回复
MainActivity.java部分
package com.example.b;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

protected DBHelper myDb;
private SQLiteDatabase dbReader,dbWriter;
private Button buttonSet;
protected boolean flag=false;
public TextView tv11,tv12,tv13,tv14,tv15,
tv21,tv22,tv23,tv24,tv25,
tv31,tv32,tv33,tv34,tv35,
tv41,tv42,tv43,tv44,tv45;
//public TextView txMon,txTus,txWed,txThurs,txFri,txSatur,txSun;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiviews();//找到view
//实例化DB
myDb=new DBHelper(this);
dbWriter=myDb.getWritableDatabase();
dbReader=myDb.getReadableDatabase();
//第一次App运行初始化数据库
intailDB();

setTextListenners();//设置点击事件
setTextContent();//从SQL读取数据
}
//找到view
private void intiviews(){
tv11=findViewById(R.id.tv11);
tv12、tv13、tv14、 tv15....tv45
(共20个tv)
buttonSet=findViewById(R.id.buttonSet);
}
//设置点击事件
private void setTextListenners(){
tv11.setOnClickListener(this);
tv12、tv13、tv14、 tv15....tv45
(共20个tv)
buttonSet.setOnClickListener(this);
}
//从SQL读取数据
private void setTextContent(){
tv11.setText(getDbContent(1));
( tv11, tv12,TV13,TV14,TV15,....
TV41,TV42,TV43,TV44,tv45 ,共20个tv)
}
//返回时刷新界面
@Override
protected void onResume(){
super.onResume();
onCreate(null);
}
//点击Text View进入courseADD
@Override
public void onClick(View v){
Intent intent=new Intent(MainActivity.this,courseAdd.class);
switch (v.getId()){
case R.id.tv11:
intent.putExtra("flag","0");
startActivity(intent);
break;
tv12、tv13、tv14、 tv15....tv45
(共20个tv,省略)
case R.id.buttonSet:
Intent intent1=new Intent(MainActivity.this,set.class);
startActivity(intent1);
break;
}
}

//通过id获取数据
public String getDbContent(int i){
String CourseName="";
String ClassRoom="";
String CourseTeacher="";
String sql="";
switch (i){
case 1:
sql="select*from course where _id='1'";
break;
( case1.....到case20 ,共20个,省略)

}
Cursor cursor=dbReader.rawQuery(sql,new String[]{});
while (cursor.moveToNext()){
CourseName=cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COURSENAME));
ClassRoom=cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.CLASSROOM));
CourseTeacher=cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COURSETEACHER));
}
//对cursor使用后进行回收、释放
cursor.close();
return CourseName+ClassRoom+CourseTeacher;
}
//第一次App运行初始化数据库
private void intailDB(){
SharedPreferences sharedPreferences=this.getSharedPreferences("share",MODE_PRIVATE);
boolean isFirstRun=sharedPreferences.getBoolean("isFirstRun",true);
SharedPreferences.Editor editor=sharedPreferences.edit();
if (isFirstRun){
for (int i=0;i<20;i++){
addDb();
}
editor.putBoolean("isFirstRun",false);
editor.apply();
}
}
//增加数据
public void addDb(){
ContentValues cv=new ContentValues();
cv.put(DBHelper.COURSENAME,"");
cv.put(DBHelper.CLASSROOM,"");
cv.put(DBHelper.COURSETEACHER,"");
dbWriter.insert(DBHelper.TABLE_NAME,null,cv);
}
//db转化px
/*public static int dip2px(Context context,float dpValue){
final float scale=context.getResources().getDisplayMetrics().density;
return (int)(dpValue*scale+0.5f);
}*/
//随机设置背景颜色

//重写返回方法实现点两次退出应用(略)

CourseAdd.java部分

package com.example.b;

import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class courseAdd extends AppCompatActivity {
//声明
private Button bSaveCourse,bDeleteCourse;
private EditText editCourseName,editClassRoom,editCourseTeacher;
public String CourseName="",//课程名称
ClassRoom="",//上课教室
CourseTeacher="";//授课教师
private String val;
private DBHelper myDb;
private SQLiteDatabase dbWriter,dbReader;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_add);
myDb=new DBHelper(this);
dbReader=myDb.getReadableDatabase();
dbWriter=myDb.getWritableDatabase();
findView();
//val获取intent的传递值
val=getIntent().getStringExtra("flag");
setOriginEditText(val);
//设置点击事件
bSaveCourse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取EditText的内容
getEditextContext();
ToastUtil.showMsg(courseAdd.this,"已设置");
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
UpdateCourse(val);//判断是哪个text的数据
finish();
}
},100);//短暂延迟后关闭导入界面
}
});
bDeleteCourse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});

}
//通过id更新数据库
private void UpdateCourse(String i){
ContentValues cv=new ContentValues();
cv.put(DBHelper.COURSENAME,CourseName);
cv.put(DBHelper.CLASSROOM,ClassRoom);
cv.put(DBHelper.COURSETEACHER,CourseTeacher);
String whereClauseString="";
switch (i){
case "0":
whereClauseString="_id='1'";
break;
( case0....case19,共20个,省略)
}
String[]whereArgs={};
dbWriter.update(DBHelper.TABLE_NAME,cv,whereClauseString,whereArgs);
}

//获取输入内容
private void getEditextContext(){
CourseName=editCourseName.getText().toString();
ClassRoom=editClassRoom.getText().toString();
CourseTeacher=editCourseTeacher.getText().toString();
}
//找到控件
private void findView(){
bSaveCourse=findViewById(R.id.buttonSaveCourse);
bDeleteCourse=findViewById(R.id.buttonDeleteCourse);
editCourseName=findViewById(R.id.editCourseName);
editClassRoom=findViewById(R.id.editClassRoom);
editCourseTeacher=findViewById(R.id.editCourseTeacher);
}
public void setOriginEditText(String s){
String CourseName="";
String ClassRoom="";
String CourseTeacher="";
String sql="";
switch (s){
case "0":
sql="select*from course where _id='1'";
break;
( case0....case19,共20个,省略)
}
Cursor cursor=dbReader.rawQuery(sql,new String[]{});
while (cursor.moveToNext()){
CourseName=cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COURSENAME));
ClassRoom=cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.CLASSROOM));
CourseTeacher=cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.COURSETEACHER));
}
cursor.close();
editCourseName.setText(CourseName);
editClassRoom.setText(ClassRoom);
editCourseTeacher.setText(CourseTeacher);
}

//提示删除功能(略)
我把主页面和增加课程信息页面全部贴出来,有部分省略,谢谢您了
zhang106209 2020-03-12
  • 打赏
  • 举报
回复
你把保存的代码和更新数据的代码贴出来看看

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧