here’s activity 1 which is a listview. when the user clicks on an item, I want the item clicked to launch an instance of a class and pass an int value to it, which will later be used in a switch.
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
switch(position){
case 0:
Intent g = new Intent(books.this, SpecificBook.class);
Bundle b = new Bundle();
b.putInt("dt", 0);
g.putExtras(b);
books.this.startActivity(g);
break;
case 1:
Intent ex = new Intent(books.this, SpecificBook.class);
Bundle b1 = new Bundle();
b1.putInt("dt", 1);
ex.putExtras(b1);
books.this.startActivity(ex);
break;
//etc.
here’s activity 2, which is supposed to retrieve the int value and call the appropriate method from the database helper class.
public class SpecificBook extends Activity {
private DatabaseHelper Adapter;
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
int dt = myBundle.getInt("dt");
@SuppressWarnings("deprecation")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listy);
ListView lv = (ListView)findViewById(R.id.listview);
Adapter = new DatabaseHelper(this);
Adapter.open();
Cursor cursor = null;
switch(dt){
case 0:cursor = DatabaseHelper.getbook1Data(); break;
case 1:cursor = DatabaseHelper.getbook2Data(); break;
//etc.
}
startManagingCursor(cursor);
etc.
The database methods are queries.
Basically, I want each item in the book class listview to run it’s own query based on the item selected and display the results.
I get a “source not found” and runtimeexception error. where am i going wrong? is there a better way to go about this?
I have already tried the “getter and setter” way to no avail. I’ve also tried the “putextra” method on the instance of the intent but that didn’t work.
The earliest you can access an Intent is in
onCreate():You should check if the Intent or Bundle is null and if you only want the one item from your Intent you can use:
Lastly, you don’t need to create a new Bundle to pass values in an Intent and it looks like you simply want to pass the
position… So you can drastically shorten youronItemClick()method: