int countervalue = i.getIntExtra("Count", 0); makes app crash - android

int countervalue = i.getIntExtra("Count", 10);
I have this line of code in a different activity when someone clicks a button they go to that activity. Without this line of code the app runs perfectly.
Here is the whole code for the activity:
package com.example.navjeevenmann.mytycoon;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class SecondActivity extends AppCompatActivity {
private ListView listView;
Intent i = getIntent();
int userchoice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
int countervalue = i.getIntExtra("Count", 0);
String[] values = {"Apple($20)- Generates $40/sec",
"Second", "Third"};
listView = (ListView) findViewById(R.id.List);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
}

initialize i = getIntent(); inside onCreate() method
right before int countervalue = i.getIntExtra("Count", 0);

you can use bundle such as
First Activity
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("int",20);
startActivity(intent);
Now in Second Activity use this
Bundle i = getIntent().getExtras();
int value = i.getInt("int");

Vyacheslav's answer is correct, but here's a little more info.
When you write:
public class SecondActivity extends AppCompatActivity {
Intent i = getIntent();
}
Your Intent i will be intialized as soon as your SecondActivity instance is created, and it will be set to the results of the getIntent() method.
If you look at the source for this method, you see:
public Intent getIntent() {
return mIntent;
}
At the time your SecondActivity instance is created, mIntent is null. So your activity is behaving as though you had written this instead:
public class SecondActivity extends AppCompatActivity {
Intent i = null;
}
The solution, as Vyacheslav said, is to initialize the i variable later on. Anywhere inside onCreate() (after the super call) is a fine place to do so, but waiting until just before your i.getIntExtra("Count", 0) call will work as well.

Related

ListView null pointer exception

I have two classes. The first creates an arraylist and an adapter.
Then passes the adapter to an other class to create the ListView.
The problem is that the Listview says it is null. any idea why?
Class 1:
package com.example.seth.greekproducts;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
SQLiteDatabase db;
Builder alert;
EditText etName;
EditText etCode;
String name;
String code;
ArrayList<String> resultslist;
Results results;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.editname);
etCode = (EditText) findViewById(R.id.editcode);
Button bSearch = (Button) findViewById(R.id.bSearch);
bSearch.setOnClickListener(this);
Button bAdd = (Button) findViewById(R.id.bAdd);
bAdd.setOnClickListener(this);
resultslist = new ArrayList<String>();
results = new Results();
db=openOrCreateDatabase("ProductsDB", Context.MODE_PRIVATE, null);
//db.execSQL("CREATE TABLE IF NOT EXISTS products(name VARCHAR, code VARCHAR);");
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bSearch:
name = etName.getText().toString();
code = etCode.getText().toString();
Cursor c=db.rawQuery("SELECT * FROM products WHERE code = '" + code + "'", null);
if(c.moveToFirst())
{
//etName.setText(c.getString(1));
//etCode.setText(c.getString(3));
resultslist.add(c.getString(0));
resultslist.add(c.getString(1));
resultslist.add(c.getString(1));
resultslist.add(c.getString(1));
adapter = new ArrayAdapter<String>(this, R.layout.simplerow, resultslist);
etName.setText(adapter.getItem(0).toString());
etCode.setText(adapter.getItem(1).toString());
results.showResults(adapter);
startActivity(new Intent(this, Results.class));
}
else
{
showInfo("Σφάλμα", "Δεν Βρέθηκε Αποτέλεσμα!");
}
break;
}
}
public void showInfo(String title, String msg){
alert = new Builder(this);
alert.setTitle(title);
alert.setMessage(msg);
alert.show();
}
}
Class 2
package com.example.seth.greekproducts;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Results extends AppCompatActivity{
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
list = (ListView) findViewById( R.id.listView );
}
public void showResults(ArrayAdapter adapter){
list.setAdapter(adapter);
System.out.println("Called!");
}}
The error is:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at Line:
list.setAdapter(adapter);
Activity_Results XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.seth.greekproducts.Results">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>
So your problem is that you are creating an activity with a constructor, like this:
results = new Results();
This won't work, you should always create activities using an Intent and startActivity(...), like you did a bit later in your code:
startActivity(new Intent(this, Results.class));
I also recommend the name suffix Activity for activities, so ResultsActivity in your case.
The main problem you will have is that you can't easily send data to your activity, because you don't have a reference to it.
You can send objects using your Intent the activity is created with.
You have the List<String> resultsList, which you want to pass onto the ResultsActivity. Do this by creating the activity with this intent:
Intent intent = new Intent(this, ResultsActivity.class);
intent.putStringArrayListExtra("results", (ArrayList<String>) resultsList);
startActivity(intent);
Now modify your ResultsActivity that it can take these results:
public class ResultsActivity extends AppCompatActivity {
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
list = (ListView) findViewById(R.id.listView);
ArrayList<String> resultsList = getIntent().getStringArrayListExtra("results");
adapter = new ArrayAdapter<String>(this, R.layout.simplerow, resultslist);
showResults(adapter);
}
public void showResults(ArrayAdapter adapter){
list.setAdapter(adapter);
System.out.println("Called!");
}
}
Activity work independent from each other. If you will call one of method of Activity into another you have to make sure that, first activity is running unless it will give you nullpointer.
So rather then sending adapter using method, Use Intent.putExtra to send data to another activity and initialize adapter there to show data in second activity.
Intent i = new Intent(MainActivity.this,Results.class );
i.putExtra("","");//put data to send to Results.class
startActivity(i);
in Result.class's onCreate method:
Intent i =getIntent();
Object obj=i.getExtra(""); // Change Object with your Parameter type
Second Method:
You can try to startActivity without sending any data from database and when you start Result Activity you can get data from database then show it into listview.
I recommended second solution as it will make you easy to code.
You called the showResults method before starting your activity
results.showResults(adapter);
startActivity(new Intent(this, Results.class));
so your list is null. You must do it after.
But the main problem is that you create a instance of an activity manually. See #Blackbelt answer

ListView doesn't seem to be working

I'm trying to create a ListView where you click on a row and it takes you to a page with the name and other details. I've spent many days trying to make it work, but at the moment all I have is that clicking the row takes you to a blank page. I've searched many questions but none of them add a title to the new page! Here's my listview activity:
package com.example.cookbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.View;
public class SecondScreenActivity extends Activity
{
ArrayList<String> RecipeList;
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.main);
// Get the reference of ListViewRecipes
ListView RecipeListView=(ListView)findViewById(R.id.mainListView);
RecipeList = new ArrayList<String>();
getRecipeNames();
// Create The Adapter with passing ArrayList as 3rd parameter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, RecipeList);
// Set The Adapter
RecipeListView.setAdapter(arrayAdapter);
// register onClickListener to handle click events on each item
RecipeListView.setOnItemClickListener(new OnItemClickListener()
{
// argument position gives the index of item which is clicked
public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
{
Intent i=new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", position);
startActivity(i);
}
});
}
void getRecipeNames()
{
RecipeList.add("Recipe1");
RecipeList.add("Recipe2");
RecipeList.add("Recipe3");
RecipeList.add("Recipe4");
RecipeList.add("Recipe5");
RecipeList.add("Recipe6");
RecipeList.add("Recipe7");
RecipeList.add("Recipe8");
RecipeList.add("Recipe9");
RecipeList.add("Recipe10");
}
}
Here's my new page activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ThirdScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
}
}
and screen2 is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView
android:id="#+id/textviewPosition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
I'm not getting any error messages in eclipse or logcat, it's just showing a blank page when I click!
Thanks for the help
edit: tried (String.valueOf(position)) instead of (position) and it now says null for every row. eg. on ThirdScreenActivity:
TextView t = ((TextView)findViewById(R.id.textviewPosition));
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(String.valueOf(position));
the reason you are not getting anything is because you are passing an int. position is an int not a string extra so the getStringExtra wont work because the extra is an int.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked,
int position, long id) {
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
Intent i = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", strText);
startActivity(i);
}
});
First of all, why are you creating the same intent and starting it twice?
You just need this:
Intent i = new Intent(SecondScreenActivity.this, ThirdScreenActivity.class);
i.putExtra("position", position);
startActivity(i);
Edit:
Secondly, are you sure your screen2 contains visible elements? You should post your XML
The problem was that you were passing an int as a extra:
i.putExtra("position", position);
and receiving a string:
Intent intent = getIntent();
String position = intent.getStringExtra("position");
t.setText(position);
...So you have to change this two lines for the following lines...
Intent intent = getIntent();
int position = intent.getExtras().getInt("position");
t.setText(String.valueOf(position));
EDIT: If you wanna pass a string then do this...
i.putExtra("position", String.valueOf(position));
...So you have to receive the string on the other activity...
Intent intent = getIntent();
String position = intent.getExtras().getString("position");
t.setText(position);

Android ListView does not update onResume

I made an Activity for searching people that also shows history of recent research.
If I long click on an item of the history it asks me if I want to delete it. If I press "Yes" it deletes the list item.
So, I write something and click to "Search" button. This brings me in another Activity with results. Here I click on result so it stores the person info and brings me in the person page.
When I come back I don't see the new person in the history.
So I overwritten onResume() but it still not work and now I cannot delete items from the history list.
Here the code:
package com.lpsmt.proffinder;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.lpsmt.R;
public class HomeActivity extends Activity
{
protected Db db = null;
protected List<ProfBean> historyProfs = null;
protected ProfListItemAdapter listAdapter = null;
protected ListView listView = null;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.db = new Db(this);
this.setContentView(R.layout.prof_finder_home);
this.historyProfs = this.db.getHistory(-1); // -1 means with no limits
this.listAdapter = new ProfListItemAdapter(HomeActivity.this, R.id.prof_finder_history_list_view, this.historyProfs);
this.listView = (ListView) this.findViewById(R.id.prof_finder_history_list_view);
listView.setAdapter(this.listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(HomeActivity.this, ProfPageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("profId", HomeActivity.this.historyProfs.get(position).getProfId());
intent.putExtras(bundle);
HomeActivity.this.startActivity(intent);
}
});
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id)
{
Resources resources = HomeActivity.this.getResources();
String title = resources.getString(R.string.prof_finder_history_delete_title);
String message = resources.getString(R.string.prof_finder_history_delete_message);
AlertDialog.Builder adb=new AlertDialog.Builder(HomeActivity.this);
adb.setTitle(title);
adb.setMessage(message);
final int positionToRemove = position;
String positive = resources.getString(R.string.prof_finder_history_delete_positive);
String negative = resources.getString(R.string.prof_finder_history_delete_negative);
adb.setNegativeButton(negative, null);
adb.setPositiveButton(positive, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ProfBean prof = HomeActivity.this.historyProfs.get(positionToRemove);
HomeActivity.this.db.deleteProf(prof.getProfId());
HomeActivity.this.historyProfs.remove(positionToRemove);
HomeActivity.this.runOnUiThread(new Runnable() {
public void run() {
HomeActivity.this.listAdapter.notifyDataSetChanged();
}
});
}});
adb.show();
return true;
}
});
}
public void searchProf(View view) throws Exception
{
EditText queryEditText = (EditText) this.findViewById(R.id.prof_finder_search_query);
String query = queryEditText.getText().toString().trim();
queryEditText.setText(query);
if (query.length() < 3) {
String message = this.getResources().getString(R.string.prof_finder_query_too_short);
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return;
}
Intent intent = new Intent(HomeActivity.this, SearchResultActivity.class);
Bundle bundle = new Bundle();
bundle.putString("query", query);
intent.putExtras(bundle);
this.startActivity(intent);
}
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.notifyDataSetChanged();
}
}
You haven't set any new data to list view. Thats why your new contact isn't added to the list after notifyDataSetChanged(). You need to add some method into adapter like
setData(List<ProfBean> data)
{
this.currentAdaptersList= data;
}
and then call notifyDataSetChanged(). So the final onResume will be :
public void onResume()
{
super.onResume();
this.historyProfs = this.db.getHistory(-1);
this.listAdapter.setData(this.historyProfs);
this.listAdapter.notifyDataSetChanged();
}
Enjoy.
And using onResume() for this task is bad idea. Is better to use onActivityResult.
notifyDataSetChanged() didn't work for me either. I was able to solve this a little bit differently:
I use OnStart() (in a derived class from Fragment)
I use setNotifyOnChange() of the ArrayAdapter:
ListView listView = (ListView) findViewById(R.id.logListView);
listView.setAdapter(logAdapter);
logAdapter.setNotifyOnChange(true);
I create the adapter once:
logAdapter = new ArrayAdapter(activity, android.R.layout.simple_list_item_1, activity.logMessages);
in onViewCreated().

ListView: Null pointer exception

Good day. I'm having some issues with my android project specifically listview. I tried searching for other information here in this site, and implemented some of the answers. However, it is still not working.
The error specifically is
NullPointerException at line 76 at MainActivity
Here is the code of my MainActivity
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity {
final ArrayList<String> studentName = new ArrayList<String>();
ArrayAdapter<String> aa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myList = (ListView) findViewById(R.id.listName);
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, studentName);
myList.setAdapter(aa);
//droid.R.id.list;
//add
Button bAdd = (Button) findViewById(R.id.addstudent);
bAdd.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
startActivity(new Intent("android.intent.action.ADDSTUDENTS"));
}
});
//edit
Button bEdit = (Button) findViewById(R.id.editstudent);
bEdit.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View x) {
startActivity(new Intent("android.intent.action.EDITSTUDENTS"));
}
});
//edit
Button bDelete = (Button) findViewById(R.id.deletestudent);
bDelete.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View x) {
startActivity(new Intent("android.intent.action.DELETESTUDENTS"));
}
});
}
public ArrayList<String> getArray(){
return studentName;
}
public void notifyArray(){
aa.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and line 76 by the way is
aa.notifyDataSetChanged();
Here is my code for the AddStudents class
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddStudents extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_student);
Button bAddStudents = (Button) findViewById(R.id.add);
final EditText et = (EditText) findViewById(R.id.student_name);
bAddStudents.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
MainActivity as = new MainActivity();
as.getArray().add(et.getText().toString());
as.notifyArray();
finish();
}
});
Button bBack = (Button) findViewById(R.id.backadd);
bBack.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
and the xml part with the list view is
<ListView
android:id="#+id/listName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
I hope you can help me cause I want to also learn what my mistakes are. I can add other information if you want.
In your AddStudents class, you're calling notifyArray() right after you instantiated MainActivity. MainActivity.onCreate() will not be called just by instantiating it.
Instantiating your MainActivity there is probably not what you want anyway (because that object will be disposed directly after the onClick handler is done).
What you want instead is to access the existing instance of MainActivity. For that, add a reference to the current instance to a static member of your MainActivity class, e.g.
public class MainActivity extends Activity {
public static MainActivity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
activity = this;
}
}
Then in your AddStudent class access it via
MainActivity.activity.notifyArray()
This is not the most beautiful way to solve your issue, but it works as long as you can be sure to only have one MainActivity instance. (If not, you could make the array itself static; or create a Singleton wrapper class for it.)
notifyArray() is being called before onCreate.
Try calling getArray().add(et.getText().toString()); and notifyArray(); inside onResume() of MainActivity and NOT from AddStudentActivity( not recommended!)
So onResume() you would ideally want to add a new student to the list, so in your case, you can retrieve the student name using a common sharable object like a hashtable or somethiing similar, make it a singleton, and use it from anywhere in the applciation
The common class may go something like:
class CommonHashtable{
private static Hashtable<String, Object> commonHashtable = null;
public static getInstance(){
if(commonHashtable == null)
commonHashtable = new Hashtable<String, Object>();
return commonHashtable;
}
on getInstance(), it returns a commonHashtable which can be used to store values temporarily!
so, add this on addbutton click event
Hashtable hash = CommonHashtable.getInstance();
hash.put("NEW_STUDENT_NAME", et.getText().toString());
and add this in you onResume() of MainActivity
Hashtable hash = CommonHashtable.getInstance();
Object studentName = (String) hash.get("NEW_STUDENT_NAME");
if(studentName != null){
notifyArray();
}

array that starts a new activity

OK, I realize this is prolly pretty basic, but im so new to this its unreal.
What I have is an array. What I want is for when a user clicks an item in the array, it opens a new activity that is specific to that item. Its a list of festivals, and when you click on one of the festivals, and when you click on it, it opens an activity that provides information about that festival.
I have no idea what I'm doing here. Im pretty sure I need to use an OnClickListener, but thats it.
Activity
package com.MADONK.LAFESTS;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
public class Home extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.main, Festivals));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
static final String[] Festivals = new String[] {
"Lake Arthur Regatta", "Contraband Days", "Iowa Rabbit Festival",
};
}
Since you're extending ListActivity, you can override onListItemClick(). You could do something like this, which gets the appropriate Festival object, and passes a member of it into an Intent, the Intent is then used to start another Activity:
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Festival item = (Festival) l.getItemAtPosition(position);
Intent i = new Intent(v.getContext(),YourFestivalDetailActivity.class);
i.putExtra("some_attribute", item.getSomeAttribute());
startActivity(i);
}
And then in the Activity you start that is meant to show the Festival detail, which in the above example is called YourFestivalDetailActivity, you should extract the Festival information from the Intent used to start it:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String someAttribute = getIntent().getStringExtra("some_attribute");
}
Note that in this example I only pass a single String via the Intent, but know that you can pass more than that via an Intent. See the docs.

Categories

Resources