我正在开发一个tasks应用程序,我想用sqldatabase保存一些数据,但我遇到了一些问题,因为当我试图保存数据时,我的应用程序崩溃了。我还希望使用列表适配器在自定义列表视图(带有自定义列表项)中显示此数据。
这是错误消息:
android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
以下是dbhelper的代码:
package com.example.taskmasterv3;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "Subtaskdata.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create Table Subtaskdata(subtaskname TEXT primary key, pri TEXT, time TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop Table if exists Userdetails");
}
public Boolean insertSubtaskData(String subtaskName, String pri, String time){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("subtaskname", subtaskName);
contentValues.put("pri", pri);
contentValues.put("time", time);
long result = db.insert("Subtaskdata", null, contentValues);
if (result < 0){
return false;
}
else
{
return true;
} }
public Boolean updateSubtaskData(String subtaskName, String pri, String time){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("subtaskname", subtaskName);
contentValues.put("pri", pri);
contentValues.put("time", time);
Cursor cursor = db.rawQuery("Select * from Subtaskdata where subtaskname = ?", new String[]{subtaskName});
if (cursor.getCount()> 0){
long result = db.update("Subtaskdata", contentValues, "name=?", new String[]{subtaskName});
if (result== -1){
return false;
}
else
{
return true;
} }
else
{
return false;
}}
public Boolean deletedata(String subtaskName){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from Subtaskdata where subtaskname = ?", new String[]{subtaskName});
if (cursor.getCount()> 0){
long result = db.delete("Subtaskdata","name=?", new String[]{subtaskName});
if (result== -1){
return false;
}
else
{
return true;
} }
else
{
return false;
}}
public Cursor getdata (){
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("Select * from Subtaskdata", null);
return cursor;
}
}
我的子任务类的代码(仅用于理解)
package com.example.taskmasterv3;
public class SubtaskPartTwo {
private String subtaskName;
private String pri;
private String time;
public SubtaskPartTwo(String subtaskName, String pri, String time) {
this.subtaskName = subtaskName;
this.pri = pri;
this.time = time;
}
public String getSubtaskName() {
return subtaskName;
}
public void setSubtaskName(String subtaskName) {
this.subtaskName = subtaskName;
}
public String getPri() {
return pri;
}
public void setPri(String pri) {
this.pri = pri;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
主类的代码:
package com.example.taskmasterv3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class TaskSummary extends AppCompatActivity {
ListView lvTaskList;
TextView tvBreak, tvBreakAfterEvery, txt1, txt2, text1, hmm;
TextView break_duration_mins;
ArrayList<SubtaskPartTwo> subtaskList = new ArrayList<>();
String subtname;
String pri;
String time;
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_summary);
lvTaskList = findViewById(R.id.lvTaskList);
tvBreak = findViewById(R.id.tvBreak);
tvBreakAfterEvery = findViewById(R.id.tvBreakAfterEvery);
txt1 = findViewById(R.id.txt1);
txt2 = findViewById(R.id.txt2);
break_duration_mins = findViewById(R.id.break_duration_mins);
text1 = findViewById(R.id.text1);
hmm = findViewById(R.id.hmm);
dbHelper = new DBHelper(this);
Cursor res = dbHelper.getdata();
while(res.moveToNext()){
subtname = dbHelper.getdata().getString(0);
pri = dbHelper.getdata().getString(1);
time = dbHelper.getdata().getString(2);
}
SubtaskDetailAdapter adapter = new SubtaskDetailAdapter(this, subtaskList);
lvTaskList.setAdapter(adapter);
SubtaskPartTwo subtaskPartTwo = new SubtaskPartTwo(subtname, pri, time);
subtaskList.add(subtaskPartTwo);
adapter.addANewSubTask(subtaskPartTwo);
double working_hours = getIntent().getIntExtra("working_hours", 1);
double working_minutes = getIntent().getIntExtra("working_minutes", 0);
double without_break_hours = getIntent().getIntExtra("without_break_hours", 1);
double without_break_minutes = getIntent().getIntExtra("without_break_minutes", 0);
double break_duration = getIntent().getIntExtra("break_duration", 20);
String a = working_hours + " h";
txt1.setText(a);
String b = working_minutes + " m";
break_duration_mins.setText(b);
String c = break_duration + " m";
txt2.setText(c);
//Mathematics
double g = working_hours * 100;
double h = g + working_minutes;
double i = h + break_duration;
double j = i / 60;
double p = (int) j;
double q = j - p;
double r = q * 60;
without_break_hours = p;
without_break_minutes = r;
String d = without_break_hours + " h";
String e = without_break_minutes + " m";
text1.setText(d);
hmm.setText(e);
}
}
列表适配器的代码:
package com.example.taskmasterv3;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class SubtaskDetailAdapter extends ArrayAdapter<SubtaskPartTwo> {
private final Context context;
private ArrayList<SubtaskPartTwo> values;
public boolean deleted;
public SubtaskDetailAdapter(Context context, ArrayList<SubtaskPartTwo> list) {
//since your are using custom view,pass zero and inflate the custom view by overriding getview
super(context, 0 , list);
this.context = context;
this.values = list;
}
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//check if its null, if so inflate it, else simply reuse it
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.task_summary_item, parent, false);
}
//use convertView to refer the childviews to populate it with data
TextView tvSubtaskName = convertView.findViewById(R.id.tvlolitaskname);
ImageView ivPri = convertView.findViewById(R.id.ivloliPri);
ImageView ivTime = convertView.findViewById(R.id.ivloliTime);
tvSubtaskName.setText(values.get(position).getSubtaskName());
if (values.get(position).getPri().equals("h"))
{
ivPri.setImageResource(R.drawable.priority_high);
}
if (values.get(position).getPri().equals("m"))
{
ivPri.setImageResource(R.drawable.priority_med);
}
if (values.get(position).getPri().equals("l"))
{
ivPri.setImageResource(R.drawable.priority_low);
}
if (values.get(position).getTime().equals("more"))
{
ivPri.setImageResource(R.drawable.time_symbol_more);
}
if (values.get(position).getPri().equals("med"))
{
ivPri.setImageResource(R.drawable.time_symbol_med);
}
if (values.get(position).getPri().equals("less"))
{
ivPri.setImageResource(R.drawable.time_symbol_less);
}
//return the view you inflated
return convertView;
}
//to keep adding the new subtasks try the following
public void addANewSubTask(SubtaskPartTwo newSubTask){
ArrayList<SubtaskPartTwo> newvalues = new ArrayList<>(this.values);
newvalues.add(newSubTask);
this.values = newvalues;
notifyDataSetChanged();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!