listview项目单击不同的一个

时间:2013-08-05 06:19:30

标签: android listview click

我正在添加记录到我的android sqlite数据库中,这些记录显示在我的列表视图中,当我点击第一个时,它们会改变颜色(选中),这很好。但当我点击中间的那个时, 最后一个被标记,当我点击最后一个应用程序“意外停止”。我想问题是,当我点击最后一个那之后有三条记录,这就是为什么它会跳,但我怎么解决呢? 这是catlog:

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>

2 个答案:

答案 0 :(得分:0)

这是一个数据库错误。转到查询数据的功能。然后在返回结果之前,关闭数据库。

答案 1 :(得分:0)

这是android buddy中列表视图的默认错误。如果你还想这样做,你必须制作自定义列表视图。

请参阅以下链接

Tutorial for Create custom list view