使用自定义游标类在ListView中添加删除按钮

时间:2016-09-05 10:15:11

标签: android sqlite listview

我正在尝试创建一个列表视图,我需要在其中添加从SIGNUP页面获取的详细信息,并在列表中显示所有SIGNUP信息。 除此之外,我需要添加一个按钮" REMOVE"可以在需要时从列表中删除任何项目。所以,我需要在每个列表项前面放一个按钮。

activity_main.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:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hsports.signuppage.MainActivity"
    android:orientation="vertical"
android:background="@color/WHITE"

    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Shaadi Mubarak"
        android:textSize="30dp"
        android:textColor="@color/RED"
        android:layout_gravity="center"
        android:id="@+id/TitleText"


        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="It is a company which aims at bringing people together and it's just a way to make this thing easier."
        android:textSize="15dp"
        android:textColor="@color/BLACK"
        android:textAlignment="center"
        android:paddingBottom="100dp"
        android:id="@+id/Title"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:lines="1"
        android:singleLine="true"

        />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"


    >

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:layout_gravity="center_horizontal"
            android:hint="Enter your Name"

            android:gravity="center_horizontal"

            android:imeOptions="actionGo"
            android:textColor="@color/BLACK"

            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"

            android:id="@+id/emailId"
            android:hint="Enter your Email Id"
            android:gravity="center_horizontal"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/password"
            android:hint="Enter Password"
            android:gravity="center_horizontal"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/cnfpassword"
            android:hint="Confirm your password"
            android:gravity="center_horizontal"
            />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RegisterMe"
            android:textStyle="bold"
            android:textSize="20dp"

            android:padding="20dp"
            android:onClick="storeInfo"
            />




</LinearLayout>

</LinearLayout>

MainActivity.java

    package com.example.hsports.listviewandmanipulation;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    String name;
    String emailId;
    String password;
    String cnfpassword;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv=(TextView)findViewById(R.id.Title);
        tv.setSelected(true);

    }

    public void storeInfo(View v)
    {
        name=((EditText)findViewById(R.id.name)).getText().toString();

        emailId=((EditText)findViewById(R.id.emailId)).getText().toString();
        password=((EditText)findViewById(R.id.password)).getText().toString();
        cnfpassword=((EditText)findViewById(R.id.cnfpassword)).getText().toString();


        SQLiteDatabase mydb=openOrCreateDatabase("usersInfo",MODE_PRIVATE,null);
        mydb.execSQL("CREATE TABLE IF NOT EXISTS usersInformation ( _id INTEGER PRIMARY KEY AUTOINCREMENT, Name varchar (20), EmailId varchar (20) , Password varchar (20) , CnfPassword varchar (20) );");
       // mydb.execSQL("INSERT INTO userInfo ( Name , EmaiId , Password , CnfPassword ) Values ( "+name+" , "+emailId+" , "+password+" , "+cnfpassword+" );");


        ContentValues values=new ContentValues();
        values.put("Name",name);
        values.put("EmailId",emailId);
        values.put("Password",password);
        values.put("CnfPassword",cnfpassword);
        mydb.insert("usersInformation",null,values);
        mydb.close();



        Intent i=new Intent(this,SecondActivity.class);
        startActivity(i);



    }


}

activity_second.xml(这里我指定了ListView标签)

<?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:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hsports.signuppage.SecondActivity"
    android:orientation="vertical"

    android:id="@+id/viewDetails"
    >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"
        >


    </ListView>



</LinearLayout>

SecondActivity.java

package com.example.hsports.listviewandmanipulation;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        SQLiteDatabase mydb=openOrCreateDatabase("usersInfo",MODE_PRIVATE,null);
        Cursor resultSet = mydb.rawQuery("Select * from usersInformation", null);
        CustomCursor cc=new CustomCursor(this,resultSet,0);

    }

}

CustomCursor.java(这是我的自定义光标类,我试图点击所选特定列表项的按钮)

       package com.example.hsports.listviewandmanipulation;

import android.Manifest;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.AvoidXfermode;
import android.support.v4.app.ActivityCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

/**
 * Created by I324671 on 9/4/2016.
 */
public class CustomCursor extends CursorAdapter {

    LayoutInflater inflater;


    public CustomCursor(Context context, Cursor c, int flags) {
        super(context, c, flags);
        inflater=LayoutInflater.from(context);

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        return inflater.inflate(R.layout.displayoflist, parent, false);

    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {





        TextView tv=(TextView)view.findViewById(R.id.name);
        final String f=cursor.getString(cursor.getColumnIndex("Name"));
        tv.setText(f);



        int rowid=Integer.parseInt(cursor.getString(cursor.getColumnIndex("_id")));
        Button bt=(Button)view.findViewById(R.id.Remove);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //String tv=((TextView) v.findViewById(R.id.name)).getText().toString();
                SQLiteDatabase mydb=SQLiteDatabase.openOrCreateDatabase("usersInfo",null);




                mydb.execSQL("DELETE FROM usersInformation where Name = '"+f+"'");
                mydb.close();

            }
        });


    }
}

这是我需要显示的列表项目:

displayoflist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"

    >

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/BLACK"
    >


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:textColor="@color/WHITE"

        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/emailId"
        android:textColor="@color/WHITE"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/password"
        android:textColor="@color/WHITE"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cnfpassword"
        android:textColor="@color/WHITE"
        />

</LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/Remove"
            android:onClick="removeData"
            android:text="Remove"
            android:layout_marginRight="0dp"
            />


    </LinearLayout>


</LinearLayout>

我无法获得列表以及显示的列表视图中的按钮(删除按钮)。

错误的完整堆栈跟踪是:

09-12 20:28:30.618 4073-4073/com.example.hsports.listviewandmanipulation E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
09-12 20:28:30.618 4073-4073/com.example.hsports.listviewandmanipulation E/SQLiteLog: (14) os_unix.c:31278: (2) open(//usersInfo) - 
09-12 20:28:30.619 4073-4073/com.example.hsports.listviewandmanipulation E/SQLiteDatabase: Failed to open database 'usersInfo'.
                                                                                           android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                                               at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                                               at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
                                                                                               at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
                                                                                               at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                                               at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                                               at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                                               at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
                                                                                               at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
                                                                                               at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                                                                                               at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
                                                                                               at com.example.hsports.listviewandmanipulation.CustomCursor$1.onClick(CustomCursor.java:57)
                                                                                               at android.view.View.performClick(View.java:5198)
                                                                                               at android.view.View$PerformClick.run(View.java:21147)
                                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-12 20:28:30.619 4073-4073/com.example.hsports.listviewandmanipulation D/AndroidRuntime: Shutting down VM
09-12 20:28:30.620 4073-4073/com.example.hsports.listviewandmanipulation E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                           Process: com.example.hsports.listviewandmanipulation, PID: 4073
                                                                                           android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                                               at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                                               at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
                                                                                               at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
                                                                                               at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                                               at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                                               at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                                               at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
                                                                                               at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
                                                                                               at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                                                                                               at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
                                                                                               at com.example.hsports.listviewandmanipulation.CustomCursor$1.onClick(CustomCursor.java:57)
                                                                                               at android.view.View.performClick(View.java:5198)
                                                                                               at android.view.View$PerformClick.run(View.java:21147)
                                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

我认为当你在CustomCursor类中覆盖newView方法时,你应该对displayoflist.xml布局进行膨胀。

@Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return inflater.inflate(R.layout.displayoflist, parent, false);
  }
在bindView中,您应该定义填充数据的位置,例如:

TextView name=(TextView)view.findViewById(R.id.name);
String nameText=cursor.getString(cursor.getColumnIndex("name"));
name.setText(nameText);

并添加:

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        SQLiteDatabase mydb=openOrCreateDatabase("usersInfo",MODE_PRIVATE,null);
        Cursor resultSet = mydb.rawQuery("Select * from usersInformation", null);
        CustomCursor cc=new CustomCursor(this,resultSet,0);

        ListView listView=(ListView)findViewById(R.id.list);
        listView.setAdapter(cc);
    }

}