2013-08-05 41 views
0

我正在添加記錄到我的列表視圖中顯示的android sqlite數據庫,當我點擊第一個時,他們改變它們的顏色(選中),這很好。但是當我點擊中間的那個, 最後一個被標記,當我點擊最後一個時,應用程序「意外停止」。我想問題是,當我點擊最後一個那裏後有三個記錄,這就是爲什麼它跳,但我怎麼能解決它? 這裏的目錄下載:listview item點擊不同的一個

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> 

回答

0

這是一個數據庫錯誤。轉到您要查詢數據的功能。然後在返回結果之前關閉數據庫。

+0

我怎麼關閉它? –

+0

db_ob.close(); 。 – msana