0

I've recently upgraded Android Studio to 2.3.3 and I've been encountering this problem ever since. If i am to deploy an application that makes use of the SQLite Database, the application does not work. It immediately shows an error that application has stopped working. All the applications that were created prior to the update, using SQLite Databases are working, however any new application that i create now is not. I dont understand why the previously working code is no longer working. Is there any way to make this work?

I've attached a sample code using the database.

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
public static ArrayList<String> Array_Names= new ArrayList<String>();
MyDBHandler db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db.getAllContacts();

    db=new MyDBHandler(this,null,null,1);
    db.addContact(new Contact("Andrew", "11111","aj2@gmail.com","Delhi"));
    db.addContact(new Contact("Karl", "22222","ks96@yahoo.com","Bangalore"));
    db.addContact(new Contact("Mike", "33333","mn@gmail.com","Chicago"));
    db.addContact(new Contact("Nathan", "12345","nj@yahoo.com","London"));

    ListAdapter myAdapter = new CustomAdapter(this, Array_Names);
    ListView mylist = (ListView) findViewById(R.id.mylist);
    mylist.setAdapter(myAdapter);

    mylist.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            }

    );

}
}

MyDBHandler

import android.content.ContentValues;
import android.database.sqlite.*;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME= "New_App.db";
public static final String TABLE_NAME= "Contacts";
public static final String TABLE_NAME1= "ContactsSorted";
public static final String COL_ID= "ID";
public static final String COL_NAME= "Name";
public static final String COL_PHNO= "PhoneNumber";
public static final String COL_EMAIL= "EMail";
public static final String COL_ADDRESS= "Address";

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String Create_query= " CREATE TABLE " + TABLE_NAME + " ( " +
            COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL_NAME + " TEXT, " +
            COL_PHNO + " TEXT, " +
            COL_EMAIL + " TEXT, " +
            COL_ADDRESS + " TEXT " +
            " ); " ;
    db.execSQL(Create_query);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(" DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public void addContact(Contact contact){

    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(COL_NAME,contact.get_name());
    values.put(COL_PHNO, contact.get_phno());
    values.put(COL_EMAIL,contact.get_email());
    values.put(COL_ADDRESS,contact.get_address());

    db.insert(TABLE_NAME,null,values);
    db.close();
}

public List<Contact> getAllContacts(){
    List<Contact> contactList=new ArrayList<Contact>();
    SQLiteDatabase db= this.getWritableDatabase();
    String Select_query= " SELECT * FROM " + TABLE_NAME1;
    Cursor cursor= db.rawQuery(Select_query,null);

    if(cursor.moveToFirst()){
        do {
            Contact contact= new Contact();
            contact.set_id(Integer.parseInt(cursor.getString(0)));
            contact.set_name(cursor.getString(1));
            contact.set_phno(cursor.getString(2));
            contact.set_email(cursor.getString(3));
            contact.set_address(cursor.getString(4));

            String name=cursor.getString(1) + "\n";
            MainActivity.Array_Names.add(name);
            contactList.add(contact);
        }while (cursor.moveToNext());
    }

    return contactList;
}
}

Contact

public class Contact {

int _id;
String _name;
String _phno;
String _email;
String _address;

public Contact(){

}


public Contact(String _address, String _email, int _id, String _name, String _phno) {
    this._address = _address;
    this._email = _email;
    this._id = _id;
    this._name = _name;
    this._phno = _phno;
}

public Contact(String _name,String _phno, String _email,String _address) {
    this._address = _address;
    this._email = _email;
    this._name = _name;
    this._phno = _phno;
}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public String get_name() {
    return _name;
}

public void set_name(String _name) {
    this._name = _name;
}

public String get_phno() {
    return _phno;
}

public void set_phno(String _phno) {
    this._phno = _phno;
}

public String get_address() {
    return _address;
}

public void set_address(String _address) {
    this._address = _address;
}

public String get_email() {
    return _email;
}

public void set_email(String _email) {
    this._email = _email;
}
}

CustomAdapter

public class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(@NonNull Context context, ArrayList<String> names) {
    super(context,R.layout.custom_row, names);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater myInflater= LayoutInflater.from(getContext());

    LayoutInflater myinflater= LayoutInflater.from(getContext());
    View customView= convertView;
    if(customView==null){
        customView=myinflater.inflate(R.layout.custom_row,parent,false);

    }
    String singleItem=getItem(position);
    TextView mytext=(TextView) customView.findViewById(R.id.mytext);
    ImageView defimage=(ImageView) customView.findViewById(R.id.myimage);
    mytext.setTextColor(Color.BLACK);
    mytext.setText(singleItem);
    defimage.setImageResource(R.mipmap.contacts_default);
    return customView;


}
}
Mehul
  • 29
  • 1
  • 2

0 Answers0