0

I have tried few solutions available on Stack Overflow. But it's still not working. I have changed the db name as well, as it was mentioned on a post in Stack Overflow.

DBHelper.java

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.HashMap;


public class DBHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "jobsBucketDBNew.db";
    //private static final int DATABASE_VERSION = 3;
    //public static final String JOBS_TABLE_PRIORITY="priority";
    public static final String JOBS_TABLE_NAME = "favtable";
    public static final String JOBS_COLUMN_ID = "id";
    public static final String JOBS_COLUMN_NAME = "name";
    public static final String JOBS_COLUMN_HEADER="header";
    public static final String JOBS_COLUMN_COMPANY="company";
    public static final String JOBS_COLUMN_CITY="city";
    public static final String JOBS_COLUMN_STATE="state";
    public static final String JOBS_COLUMN_COUNTRY="country";
    public static final String JOBS_COLUMN_FORMATEDLOCATION="formattedLocation";
    public static final String JOBS_COLUMN_SOURCE="source";
    public static final String JOBS_COLUMN_DATE="date";
    public static final String JOBS_COLUMN_SNIPPET="snippet";
    public static final String JOBS_COLUMN_URL="url";
    public static final String JOBS_COLUMN_ONMOUSEDOWN="onmousedown";
    public static final String JOBS_COLUMN_LATITUDE="latitude";
    public static final String JOBS_COLUMN_LONGITUDE="longitude";
    public static final String JOBS_COLUMN_JOBKEY="jobkey";
    public static final String JOBS_COLUMN_SPONSORED="sponsored";
    public static final String JOBS_COLUMN_EXPIRED="expired";
    public static final String JOBS_COLUMN_FORMATTEDLOCATIONFULL="formattedLocationFull";
    public static final String JOBS_COLUMN_FORMATTEDRELATIVETIME="formattedRelativeTime";
    public static final String JOBS_COLUMN_CHECKED ="checked";
    //SQLiteDatabase db= this.getWritableDatabase();
    private HashMap hp;


    public DBHelper(Context context)
    {

        super(context, DATABASE_NAME , null, 3);

       // this.onCreate(db);
    }

    @Override

    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

       /* String create_table=  "create table" +" " + JOBS_TABLE_NAME  +
                        "("+ JOBS_COLUMN_ID +" integer primary key autoincrement," + JOBS_COLUMN_HEADER + " Text," + JOBS_COLUMN_NAME + " Text," + JOBS_COLUMN_COMPANY + " Text," + JOBS_COLUMN_CITY +" Text," + JOBS_COLUMN_STATE + " Text," + JOBS_COLUMN_COUNTRY + " Text," + JOBS_COLUMN_FORMATEDLOCATION + " Text," + JOBS_COLUMN_SOURCE + " Text," + JOBS_COLUMN_DATE+ " Text," + JOBS_COLUMN_SNIPPET+ " Text," + JOBS_COLUMN_URL + " Text," + JOBS_COLUMN_ONMOUSEDOWN + " Text," + JOBS_COLUMN_LATITUDE + " Text," + JOBS_COLUMN_LONGITUDE + " Text," + JOBS_COLUMN_JOBKEY + " Text," + JOBS_COLUMN_SPONSORED +" Text,"+ JOBS_COLUMN_EXPIRED +" Text," +JOBS_COLUMN_FORMATTEDLOCATIONFULL+ " Text," + JOBS_COLUMN_FORMATTEDRELATIVETIME + " Text," + JOBS_COLUMN_CHECKED +" Text" + ");";
        db.execSQL(create_table);*/
        db.execSQL(
                        "create table" +" " + JOBS_TABLE_NAME  +
                        "("+ JOBS_COLUMN_ID +" Integer primary key autoincrement," + JOBS_COLUMN_HEADER + " Text," + JOBS_COLUMN_NAME + " Text," + JOBS_COLUMN_COMPANY + " Text," + JOBS_COLUMN_CITY +" Text," + JOBS_COLUMN_STATE + " Text," + JOBS_COLUMN_COUNTRY + " Text," + JOBS_COLUMN_FORMATEDLOCATION + " Text," + JOBS_COLUMN_SOURCE + " Text," + JOBS_COLUMN_DATE+ " Text," + JOBS_COLUMN_SNIPPET+ " Text," + JOBS_COLUMN_URL + " Text," + JOBS_COLUMN_ONMOUSEDOWN + " Text," + JOBS_COLUMN_LATITUDE + " Text," + JOBS_COLUMN_LONGITUDE + " Text," + JOBS_COLUMN_JOBKEY + " Text," + JOBS_COLUMN_SPONSORED +" Text,"+ JOBS_COLUMN_EXPIRED +" Text," +JOBS_COLUMN_FORMATTEDLOCATIONFULL+ " Text," + JOBS_COLUMN_FORMATTEDRELATIVETIME + " Text," + JOBS_COLUMN_CHECKED +" Text" + ");"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + JOBS_TABLE_NAME + ";");
        onCreate(db);
        //db.execSQL("DROP TABLE IF EXISTS priorities");
        //onCreate(db);
    }

    public boolean insertContact( String header, String name,String company,String city,String state,String country,String formattedLocation,String source,String date,String snippet,String url,String onmousedown,String latitude,String longitude,String jobkey,String sponsored,String expired, String formattedLocationFull,String formattedRelativeTime)
    {
        SQLiteDatabase db =  this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
       // contentValues.put(JOBS_COLUMN_ID,id);
        contentValues.put(JOBS_COLUMN_HEADER,header);
        contentValues.put(JOBS_COLUMN_NAME, name);
        contentValues.put(JOBS_COLUMN_COMPANY, company);
        contentValues.put(JOBS_COLUMN_CITY, city);
        contentValues.put(JOBS_COLUMN_STATE, state);
        contentValues.put(JOBS_COLUMN_COUNTRY, country);
        contentValues.put(JOBS_COLUMN_FORMATEDLOCATION, formattedLocation);
        contentValues.put(JOBS_COLUMN_SOURCE, source);
        contentValues.put(JOBS_COLUMN_DATE, date);
        contentValues.put(JOBS_COLUMN_SNIPPET, snippet);
        contentValues.put(JOBS_COLUMN_URL, url);
        contentValues.put(JOBS_COLUMN_ONMOUSEDOWN, onmousedown);
        contentValues.put(JOBS_COLUMN_LATITUDE, latitude);
        contentValues.put(JOBS_COLUMN_LONGITUDE, longitude);
        contentValues.put(JOBS_COLUMN_JOBKEY, jobkey);
        contentValues.put(JOBS_COLUMN_SPONSORED, sponsored);
        contentValues.put(JOBS_COLUMN_EXPIRED, expired);
        contentValues.put(JOBS_COLUMN_FORMATTEDLOCATIONFULL, formattedLocationFull);
        contentValues.put(JOBS_COLUMN_FORMATTEDRELATIVETIME, formattedRelativeTime);
       // contentValues.put(JOBS_COLUMN_CHECKED, checked);
        long c = db.insert("favourites ", null, contentValues);
        if(c!=-1)
         return true;
        else
            return false;
    }
 public Cursor getData(int id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from "+JOBS_TABLE_NAME+"  where id="+id+";", null );
        return res;
    }

   /* public int numberOfRows(){
        SQLiteDatabase db = this.getReadableDatabase();
        int numRows = (int) DatabaseUtils.queryNumEntries(db, JOBS_TABLE_NAME);
        return numRows;
    }*/

    public boolean updateContact (Integer id, String name)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);

        db.update("favtable", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
        return true;
    }

    public Integer deleteContact (Integer id)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete("favtable",
                "id = ? ",
                new String[] { Integer.toString(id) });
    }

    public ArrayList<String> getAllCotacts()
    {
        ArrayList<String> array_list = new ArrayList<String>();

        //hp = new HashMap();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from favtable", null );
        res.moveToFirst();

        while(res.isAfterLast() == false){
            array_list.add(res.getString(res.getColumnIndex(JOBS_COLUMN_NAME)));
            res.moveToNext();
        }
        return array_list;
    }
}

manifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="portfolio.first_app.practice.com.indeedjobactivity3">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DetailsActivity"
            android:label="Single Item Selected"></activity>
        <activity android:name=".MarkAsFav"
            android:label="Mark as Favourite"></activity>
        <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

MarkAsFav.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MarkAsFav extends Activity {

    private DBHelper mydb;

    TextView header;

    int id_To_Update = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mark_fav_layout);
        header = (TextView) findViewById(R.id.editTextName);

        mydb = new DBHelper(this);
        mydb.getWritableDatabase();
        Button b = (Button) findViewById(R.id.button1);
        b.setVisibility(View.VISIBLE);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            int value = extras.getInt("id");

            if (value > 0) {
                //means this is the view part not the add contact part.
              /*  Cursor rs = mydb.getData(value);
                id_To_Update = value;
                rs.moveToFirst();

                String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));

                if (!rs.isClosed()) {
                    rs.close();
                }
                Button b = (Button) findViewById(R.id.button1);
                b.setVisibility(View.INVISIBLE);

                header.setText( nam);
                header.setFocusable(false);
                header.setClickable(false);*/

            }
        }
    }


    public void run(View view) {
      Bundle extras = getIntent().getExtras();
        if (extras != null) {

            //int value     =   extras.getInt("id");
            String headerValue =header.getText().toString();
            String value1 =   extras.getString("title");
            String value2 =   extras.getString("company");
            String value3 =   extras.getString("city");
            String value4 =   extras.getString("state");
            String value5 =   extras.getString("country");
            String value6 =   extras.getString("formattedLocation");
            String value7 =   extras.getString("source");
            String value8 =   extras.getString("date");
            String value9 =   extras.getString("snippet");
            String value10=   extras.getString("url");
            String value11=   extras.getString("onmousedown");
            String value12=   extras.getString("latitude");
            String value13=   extras.getString("longitude");
            String value14=   extras.getString("jobkey");
            String value15=   extras.getString("sponsored");
            String value16=   extras.getString("expired");
            String value17=   extras.getString("formattedLocationFull");
            String value18=   extras.getString("formattedRelativeTime");
           // String value19="0";


            Log.e("ERROR", "Inside run and checking Value and val");

              // if (mydb.insertContact("header", "name", "company","city","state","country","formattedLocation","source","date","snippet","url","onmousedown","latitude","longitude","jobkey","sponsored","expired","formattedLocationFull","formattedRelativeTime","checked")){
            if   (mydb.insertContact(headerValue, value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12,value13,value14,value15,value16,value17,value18)){
                    Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_SHORT).show();
                    //Log.e("ERROR", "insert contact errors");
                } else {
                    Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
                }
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
            //}
        }
    }
}

I am following an online tutorial to do this.

halfer
  • 19,824
  • 17
  • 99
  • 186
SOURAV
  • 73
  • 8

0 Answers0