I have a list view with couple of items and i set this function get call when a row in the list view is clicked.
I want to open new activity and send him an object from an array of objects.
I have a problem with this line :
Intent i = new Intent(this, Item_Activity.class);
because the this is now no the activity.
this is the code:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent i = new Intent(this, Item_Activity.class);
Item item = m_items.get(position);
i.putExtra("object", item);
startActivity(i);
}
});
Problem:
You are passing the wrong context in
Intent i = new Intent(this, Item_Activity.class);
Solution:
Use : YourActivityName.this instead if using simply this
eg. Intent i = new Intent(CurrentActivityName.this, Item_Activity.class);
Add ActivityName.this instead of this only,
Intent i = new Intent(ActivityName.this, Item_Activity.class);
is your Item is parcelable, if not make this object parcelable by following link:
http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html
Your object has to be either serializable or parcelable and I don't think you can pass
an array into an intent read this - How to pass an object from one activity to another on Android
more to that
passing an array in intent android
Related
I'm having trouble figuring out how to pass information through an Activity switch.
I currently have an Activity where I can add a client to the database, and I have an Activity to view all clients in a ListView.
What I want is that when the user clicks on a client in the ListView for it to go back to the addclient Activity, with all the fields filled in from the database.
here is the Activity where the user views the clients
public class ViewClientActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.viewclient_activity);
DBHandler handler = new DBHandler(this, null, null, 1);
SQLiteDatabase db = handler.getWritableDatabase();
final Cursor ClientCursor = db.rawQuery("SELECT * FROM clients", null);
ListView allClients = (ListView) findViewById(R.id.allClients);
final ClientCursorAdapter clientAdapter = new ClientCursorAdapter(this, ClientCursor);
allClients.setAdapter(clientAdapter);
allClients.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Intent AddClientActivity = new Intent(getApplicationContext(), AddClientActivity.class);
startActivity(AddClientActivity);
}
});
}
}
You need to make the client class information implements Parcelable class. This way you will be able to add that information into the Intent you use to open the second Activity. From the second activity, you can get this information from the bundle obtained by calling getIntent()
To add the Parcelable data into de intent use intent.putExtra() and to get it from it use intent.getParcelableExtra()
when he clicks on a client in the listview for it to go back to the addclient activity but with all the fields filled in from the database
When you "go back", you are doing a SELECT *, so everything will be selected and updated correctly, assuming it was updated in the database.
Your task exists within the AddClientActivity to create a new DBHandler and update your data accordingly.
If you need to pass anything, it would be the ID of the row that was clicked, which you can use setTag within the ClientCursorAdapter class's getView method on the inflated view to put. Then, (Long) view.getTag() to receive in the click.
Also, please use getReadableDatabase() for CursorAdapter
This is what i ended up doing to pass the information i needed
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
IMAGES = imagesPath[0] + "|" + imagesPath[1] + "|" + imagesPath[2] + "|" +
imagesPath[3] + "|" + imagesPath[4] + "|" + imagesPath[5];
Intent intent = new Intent(AddClientActivity.this, ImageViewActivity.class);
intent.putExtra("IMAGES", IMAGES);
intent.putExtra("POSITION", position);
startActivity(intent);
}
});
I open the IndividualCouponsActivity from the fragment foodCouponsFragment within the parent CouponsActivity. I open the IndividualCouponsActivity with an Intent. After opening it, I want to edit the textViews and ImageViews of the IndividualCouponActivity. It should be noted that the IndividualCouponActivity is not a parent of the fragment.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Coupon coupon = foodCoupons.get(position);
Intent foodCouponsIntent = new Intent(getActivity(), IndividualCouponActivity.class);
startActivity(foodCouponsIntent);
IndividualCouponActivity activity = new IndividualCouponActivity();
activity.setValue(coupon.getCouponValue());
activity.setCompany(coupon.getCompanyName());
activity.setInfo(coupon.getDescription());
activity.setPts(coupon.getPts());
activity.setQr(coupon.getPicImageResourceId());
}
});
However, when I run the app, clicking on the listView makes the app shut down. This is what the log says at that time:
FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImplBase.
I suspect that this is rooting from my use of the new IndividualCouponActivity activity to access the class methods. Thanks!
Don't create your activity with new...
Pass the Coupon object to your activity like this:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("coupon", coupon); // or putSerializable()
intent.putExtras(bundle);
intent.setClass(context, IndividualCouponActivity.class);
context.startActivity(intent);
In your activity in onCreate() you can get the passed coupon with:
getIntent().getExtras().getParcelable("coupon"); // or putSerializable()
Then set the values to your views.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Coupon coupon = (Coupon) adapterView.getItemAtPosition(position);
Intent foodCouponsIntent = new Intent(getActivity(), IndividualCouponActivity.class);
Bundle extras = new Bundle();
extras.put("value", coupon.getCouponValue());
extras.put("companyName", coupon.getCompanyName());
extras.put("description",coupon.getDescription());
extras.put("pts", coupon.getPts());
extras.put("picImageResourceId", coupon.picImageResourceId());
foodCouponsIntent.putExtras(extras);
startActivity(foodCouponsIntent);
}
});
and then in your IndividualCouponActivity's onCreate:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get reference to views...
textViewValue = (TextView) findViewById(R.id.textViewValue);
//do this for all views....
//if all objects are String values, if not you know what you have to do...
//here you get values from extras
Bundle data = getIntent().getExtras();
String value = data.getStringExtra("value");
String companyName = data.getStringExtra("companyName");
String description = data.getStringExtra("description");
String pts = data.getStringExtra("pts");
String picImageResourceId = data.getStringExtra("picImageResourceId");
//here update UI views
//example
textViewValue.setText(value);
//do this for all views....
}
maybe there is some typo mistake, but this is the solution...
You can pass data between both activities using "putExtra"
Intent intent = new Intent(getBaseContext(), IndividualCouponActivity.class);
intent.putExtra("COUPON_COMPANY_NAME", coupon.getCompanyName());
startActivity(intent);
I'm trying to print a string from ArrayList in my another Activity using ListView. I passed a key and values in my intent variable. Using this.
MainActivity
ArrayList<String> arrayList = new ArrayList<String>();
Intent intent = new Intent(getApplicationContext(), ViewCart.class);
mealOneIncrement++;
quantityOne.setText(Integer.toString(mealOneIncrement));
mealOneCost = mealOneIncrement * mealOnePrice;
price.setText(Double.toString(mealOneCost));
String mealOneSummayName = name.toString();
String mealOneSummaryDescription = description.toString();
String mealOneSummaryQuantity = String.valueOf(mealOneIncrement);
String mealOneSummaryCost = String.valueOf(mealOneCost);
arrayList.add(mealOneSummayName);
arrayList.add(mealOneSummaryDescription);
arrayList.add(mealOneSummaryQuantity);
arrayList.add(mealOneSummaryCost);
intent.putExtra("data", arrayList);
As you can see here I passed arrayList variable as value for able to passed this from another Activity.
SecondActivity
ListView listView = (ListView) findViewById(R.id.listView);
String[] mealSummary = {};
ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mealSummary);
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("data");
listView.setAdapter(adapter);
I'm little bit confuse in this part String data = bundle.getString("data"); I'm not sure if I'm passing a correct variable to my ListView so I can able to show the arrayList variable in my ListView. Am I doing it wrong? Any help would appreciated :)
There are some problems with your code:
intent.putExtra("data", arrayList);
you are putting inside the bundle an arrayList of String, but then
String data = bundle.getString("data");
you are getting just a String. You need to use bundle.getStringArrayList("data").
ArrayList<String> data = bundle.getStringArrayList("data")
Also (not related to your problem), I recommend to use
Intent intent = new Intent(this, ViewCart.class);
instead of
Intent intent = new Intent(getApplicationContext(), ViewCart.class);
Pass Arraylist as putStringArrayListExtra,
Intent intent = new Intent(this, ViewCart.class);
intent.putStringArrayListExtra("data", arrayList);
startActivity(intent);
Get your ArrayList using getStringArrayListExtra,
arrayList = getIntent().getStringArrayListExtra("data");
You could modify this a bit.
In your secondActivity get the data like
ArrayList<String> data = getIntent().getSerializableExtra("data");
Now you got your data in an arrayList. If you wish to add this data to mealSummary then use,
mealSummary = (String[]) data.toArray();
Don't forget to call adapter.notifyDataSetChanged()
Can someone help me with this error that I keep getting?
The program that I'm trying to make is online streaming radio.
I need to return the value "value" so i can use it in another java class.
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ch);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public int onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getApplicationContext(), mPlayer.class);
i.putExtra("name", ch.get(position));
int value = listView.getItemAtPosition(position);
startActivity(i);
return value;
}
}
The problem occurs in this line:
int value = listView.getItemAtPosition(position);
getItemAtPosition() returns an Object, so you must convert it to an int before assigning it to an int variable. You should do this only if you're sure that your listView contains integers, otherwise you'll get a ClassCastException. Do it like this:
int value = (int)listView.getItemAtPosition(position);
Also, if you startActivity() before using the value anywhere, then it does not make much sense in getting it. Use putExtra to pass it to your next activity if you wish to use it there.
I want to open an activity depends on the value user select on a ListView. If user select a list item that have type1 value it should open acitvity1 (activity1.class), if the list item belongs to type2 it should open acitvity2 (activity2.class). I still wants to get the id value.
Can some one please help me to get more than one values in a list. (not just the id value) or a work around for this.
following is my code;
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//TODO:need to assign the value here
String mEdit = .......;
if (mEdit.equals("type1")){
Intent i = new Intent(this, activity1.class);
i.putExtra(DbAdapter.KEY_ROWID, id );
startActivityForResult(i, ACTIVITY_EDIT);
}else if (mEdit.equals("type2")){
Intent i = new Intent(this, activity2.class);
i.putExtra(KEY_ROWIDA, id );
startActivityForResult(i, ACTIVITY_EDIT);
};
}
Thanks in advance
You can call getListView().getItemAtPosition(int) on your ListActivity and give it the index of the list item. You can then cast it to whatever the backing object is and perform any operations on that Object. Example from one of my own apps:
ForumDescriptor selected = (ForumDescriptor) getListView().getItemAtPosition(position);
ForumDescriptor has some information about an individual subforum (it's a forums viewer for a website). Each one is an item in my list.
This could work on the situation,
...
//TODO:need to assign the value here
TextView tv = (TextView)v.findViewById(R.id.itemname);
String category = tv.getText().toString();
...
The 'R.id.itemname' is the id attribute of list item defined in layout xml.
Here is the working code;
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//TODO:need to assign the value here
Cursor mycursor = (Cursor) getListView().getItemAtPosition(position);
//1 is the field position
String mEdit = mycursor.getString(1);
if (mEdit.equals("type1")){
Intent i = new Intent(this, activity1.class);
i.putExtra(DbAdapter.KEY_ROWID, id );
startActivityForResult(i, ACTIVITY_EDIT);
}else if (mEdit.equals("type2")){
Intent i = new Intent(this, activity2.class);
i.putExtra(KEY_ROWIDA, id );
startActivityForResult(i, ACTIVITY_EDIT);
};
}