0

I am trying to make a list view in which I need to add the details fetched from the SIGNUP page and display all the SIGNUP information in the list. Along with this I need to add a button "REMOVE" which can remove any item from the list when needed. So, I need a button in front of every list item.

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(here I have specified the ListView tag)

<?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(This is my custom cursor class in which I am trying to click on the button of specific list item selected)

       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();

            }
        });


    }
}

This is the list items I need to display:

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>

I am not able to get the list as well as the button (Remove button) in the listview displayed.

The full stack trace of error is:

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)
shikher.mishra
  • 155
  • 8
  • 24

1 Answers1

0

I think you should inflate displayoflist.xml layout when you override newView method in CustomCursor class.

@Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return inflater.inflate(R.layout.displayoflist, parent, false);
  }

in bindView you should define where to populate your data for example:

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

and add this:

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);
    }

}
matejko219
  • 1,601
  • 10
  • 14
  • I have made the changes in the code as suggested by you but still I am not getting any item in the list view. – shikher.mishra Sep 05 '16 at 10:38
  • In bindView you should define what data from your cursor should be added to TextViews. In your pasted code you only define button behaviour. – matejko219 Sep 05 '16 at 10:43
  • i have made the changes as suggested by you. But still i am not getting the output. The changes which I have made are listed above as suggested by you. Please have a look. – shikher.mishra Sep 05 '16 at 18:33
  • try to add my last modification – matejko219 Sep 06 '16 at 06:06
  • I am getting the list now but now when i am clicking on the "Remove" button then its giving me this error:FATAL EXCEPTION: main Process: com.example.hsports.listviewandmanipulation, PID: 7578 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference – shikher.mishra Sep 06 '16 at 09:47
  • try this: `String tv=((TextView) view.findViewById(R.id.name)).getText().toString();` or use rowid as parameter instead of name text – matejko219 Sep 06 '16 at 10:49
  • I have made some changes in the MainActivity.java and CustomCursor.java for mydb.close() and I am not getting the above error now. Now when i press the "Remove" button then I am getting a new error.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) – shikher.mishra Sep 06 '16 at 14:28
  • I think it can be permission issue. Read [this](http://stackoverflow.com/questions/17034511/android-database-sqlite-sqlitecantopendatabaseexception-unknown-error-code-14). Maybe it would help – matejko219 Sep 07 '16 at 06:09
  • Where do I add these runtime exceptions functions in my code.(onRequestPermissionsResult,requestPermissions) . The parameters it need are different and how can I call it from bindView() or newView(). – shikher.mishra Sep 08 '16 at 04:44
  • I have no idea. Maybe you should consider writing your own database class which extends `SQLiteOpenHelper`. Read [this](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html) – matejko219 Sep 08 '16 at 06:43
  • I just dont get it that why am I getting this error. All the statements are perfect in the way they are written and seems good to go, but still I am getting this error. Please help me to solve this. – shikher.mishra Sep 12 '16 at 14:06
  • Please edit your post and add your latest modifications. It will help me to know how your code looks right now. And paste stack trace when error occured. – matejko219 Sep 12 '16 at 14:36
  • I have edited the question above with the code I am using as well as the full stack trace which I have got from the android studio. Please help me with this now. – shikher.mishra Sep 12 '16 at 15:01
  • I don't see what can cause that error. Maybe you should open your database in the same way in both activities. In first you use `openOrCreateDatabase("usersInfo",MODE_PRIVATE,null);`. – matejko219 Sep 12 '16 at 19:57
  • for "MODE_PRIVATE" it is displaying that "the variable cannot be resolved".Now what should I do? – shikher.mishra Sep 13 '16 at 10:58
  • The error displayed is "Error:(59, 92) error: incompatible types: int cannot be converted to CursorFactory". It is not expecting this value. – shikher.mishra Sep 13 '16 at 14:45
  • sorry but I don't have any idea how can I help you. – matejko219 Sep 14 '16 at 06:28