我正在将记录添加到我的列表视图上显示的我的 android sqlite 数据库中,当我单击第一个时,它们会更改它们的颜色(已选择),这很好。但是当我单击中间的时,最后一个被标记,当我单击最后一个时,应用程序“意外停止”。我想问题是当我单击最后一个时,之后有三个记录,这就是它跳转的原因,但我该如何解决呢?这是目录:
08-04 18:13:42.976: E/Database(1316): close() was never explicitly called on database '/data/data/com.example.lamder/databases/REGISTRATION_DB'
08-04 18:13:42.976: E/Database(1316): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
08-04 18:13:42.976: E/Database(1316): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
08-04 18:13:42.976: E/Database(1316): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
08-04 18:13:42.976: E/Database(1316): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
这是我的代码
package com.example.lamder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class GetActivity extends Activity {
RegistrationAdapter adapter_ob;
RegistrationOpenHelper helper_ob;
SQLiteDatabase db_ob;
ListView nameList;
Button registerBtn;
Cursor cursor;
static boolean[] check=new boolean[100];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getact);
nameList = (ListView) findViewById(R.id.lv_name);
registerBtn = (Button) findViewById(R.id.btn_register);
adapter_ob = new RegistrationAdapter(this);
cursor=adapter_ob.queryName();
Context context = getApplicationContext();
CharSequence text = "select questions by clicking on them";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
for(int i=0;i<check.length;i++)
check[i]=false;
String[] from = { helper_ob.FNAME, helper_ob.LNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname };
cursor = adapter_ob.queryName();
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
nameList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(check[position]==false)
{
check[position]=true;
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#40FF00"));
}
else
{
check[position]=false;
parent.getChildAt(position).setBackgroundColor(Color.TRANSPARENT);
}
cursor.close();
}
});
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{ String ques[]=new String[cursor.getCount()];
int i=0;
cursor.moveToFirst();
while(!cursor.isAfterLast() )
{
ques[i]= cursor.getString(cursor.getColumnIndex(helper_ob.FNAME));
i++;
cursor.moveToNext();
}
i=0;
String ans[]=new String[cursor.getCount()];
cursor.moveToFirst();
while(!cursor.isAfterLast() )
{
ans[i]= cursor.getString(cursor.getColumnIndex(helper_ob.LNAME));
i++;
cursor.moveToNext();
}
int counter=0;
for(int t=0;t<check.length;t++)
{
if(check[t]==true)
counter++;
}
String[] ques1=new String[counter];
String[] ans1=new String[counter];
int count=0;
for(int t=0;t<ans.length;t++)
{
if(check[t]==true)
{
ques1[count]=ques[t];
ans1[count]=ans[t];
count++;
}
}
if(counter>0)
{
Intent intent=new Intent(GetActivity.this,ThirdActivity.class);
intent.putExtra("Answer",ans1);
intent.putExtra("Question",ques1);
intent.putExtra("truecount",0);
intent.putExtra("falsecount",0);
startActivity(intent);
}
else
{
Context context = getApplicationContext();
CharSequence text = "select questions by clicking on them";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
});
}
@Override
public void onResume()
{
super.onResume();
cursor.requery();
}
}
和 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background = "@drawable/backnote"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv_name"
android:layout_width="wrap_content"
android:layout_height="337dp"
android:layout_weight="0.63" >
</ListView>
<Button
android:id="@+id/btn_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start" />
</LinearLayout>
和 row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="75dp" >
<TextView
android:id="@+id/tv_fname"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imageView1"
android:text="ans" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tv_lname"
android:layout_marginLeft="44dp"
android:layout_toRightOf="@+id/tv_fname" >
</RelativeLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/blackbar" />
<TextView
android:id="@+id/tv_lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv_fname"
android:text="ques" />
</RelativeLayout>
</LinearLayout>