-1

I want to populate a list view based on grid item click. If user clicks the first grid item I want to show the list view according to it. I have used a pre populate SQLite database here.

What I tried is , to get item click id in first activity and pass it to DB Class. This is what I'm getting when debugging the programme.

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.example.sra.hellosrilanka.DBAccess.getQuotes(DBAccess.java:51)
        at com.example.sra.hellosrilanka.ContactView.onCreate(ContactView.java:29)

DBAccess Class:

public class DBAccess extends Activity {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DBAccess instance;
String passedVar=null;

public DBAccess(Context context) {

    this.openHelper =new HelloDatabase(context);
}

public static DBAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DBAccess(context);
    }
    return instance;
}

public void open() {
    this.database = openHelper.getWritableDatabase();
}

public void close() {
    if (database != null) {
        this.database.close();
    }
}



public List<String> getQuotes() {
    List<String> list = new ArrayList<>();
    Integer value;

    Bundle extras = getIntent().getExtras();
    String a = extras.getString("ID_EXTRA");


    if(extras != null)
    {
        Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id = \""+ a +"\"" , null);
       /* Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id="+a, null);*/
       /*Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);*/
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(0));
            cursor.moveToNext();
            cursor.close();
        }
    }

  /*  if (passedVar != null) {


    }*/

    return list;
}}

Contact View Class:

public class ContactView extends Activity {
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);


    this.listView = (ListView) findViewById(R.id.listView);
    DBAccess databaseAccess =  DBAccess.getInstance(this);
    databaseAccess.open();
    List<String> quotes = databaseAccess.getQuotes();
    databaseAccess.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
    this.listView.setAdapter(adapter);

}

}

Main activity class

public class MainActivity extends AppCompatActivity {

GridView grid;
Toolbar toolbar;
private ListView listView;

String [] result;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    grid = (GridView) findViewById(R.id.grid);


   toolbar=(Toolbar)findViewById(R.id.toolBar);
   setSupportActionBar(toolbar);

   grid.setAdapter(new CustomAdapter(this));
            grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    /*Intent intent=new Intent(MainActivity.this,ContactView.class);
                    intent.putExtra("catid", id);
                    startActivity(intent);*/

                    Intent i=new Intent(MainActivity.this ,ContactView.class);
                    i.putExtra("ID_EXTRA",String.valueOf(id));
                    startActivity(i);

                }




            });}}
codehesh
  • 875
  • 10
  • 29
  • 1
    main problem is `DBAccess extends Activity` + `new DBAccess(context)` ... you should never call use new operator on the class which extends `Activity` by yourself – Selvin Jun 01 '16 at 15:55

3 Answers3

3

First of all DBAccess does not required to extends Activity, remove it.Update your getQuotes

public List<String> getQuotes(String id) {
    List<String> list = new ArrayList<>();
    Integer value;


    if(id != null)
    {
        Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id = \""+ id +"\"" , null);
       /* Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id="+a, null);*/
       /*Cursor cursor = database.rawQuery("SELECT org_name FROM org_name WHERE category_id='ID_EXTRA'", null);*/
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(0));
            cursor.moveToNext();
            cursor.close();
        }
    }

  /*  if (passedVar != null) {


    }*/

    return list;
}

And get value from intent in ContactView.

public class ContactView extends Activity {
private ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);


    this.listView = (ListView) findViewById(R.id.listView);
    DBAccess databaseAccess =  DBAccess.getInstance(this);
    databaseAccess.open();
    List<String> quotes = databaseAccess.getQuotes(getIntent().getStringExtra("ID_EXTRA"));
    databaseAccess.close();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, quotes);
    this.listView.setAdapter(adapter);

}
USKMobility
  • 5,721
  • 2
  • 27
  • 34
  • Thank you @USKMobility. Now it's working. When I click a grid which has items more than 1 in the list , app is automatically crashing. Is there anything wrong here? list.add(cursor.getString(0)); – codehesh Jun 02 '16 at 06:56
0

Your DBAccess class isn't being initiated by an intent, therefore there is no intent to get.

You should get the intent and the bundle in your ContactView class and then pass the string to your getQuotes function in your DBAccess class.

Jay
  • 324
  • 2
  • 14
0

You just passed the instance of ContactViewActivity to the constructor of DBAccess and then didn't keep a reference to it.

ContactViewActivity.onCreate:

DBAccess databaseAccess =  DBAccess.getInstance(this);

DBAccess.getInstance:

public static DBAccess getInstance(Context context) {
    if (instance == null) {
        instance = new DBAccess(context);  // ok let's take a look into the constructor
    }
    return instance;
}

DBAccess.(constructor):

public DBAccess(Context context) {

    this.openHelper =new HelloDatabase(context);
    // you will lose a reference to this context when returning
}

DBAccess.getQuotes:

public List<String> getQuotes() {
    List<String> list = new ArrayList<>();
    Integer value;

    Bundle extras = getIntent().getExtras(); // now, get intent from what?
    ...
}
nexus5x
  • 457
  • 4
  • 13