I am currently working on a movie review app. I am trying to pass data from a Recyclerview to another recyclerview in a new activity. Currently I know how to pass data from Recyclerview to textview and imageview. However I looked online and also tried codes but still I could not pass data from recyclerview to recyclerview. What I am trying to pass is a String ArrayList that contains urls. How could I pass the data? Please help me TY! I just recently started on android studio
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.asBitmap()
.load(mImageUrls.get(position))
.into(holder.image);
holder.name.setText(mNames.get(position));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick: Clicked on an image: " + mNames.get(position));
Toast.makeText(mContext, mNames.get(position), Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(v.getContext(), MovieDetails.class);
//v.getContext().startActivity(intent);
Intent intent = new Intent(mContext, MovieDetails.class);
intent.putExtra("image_url", mImageUrls.get(position));
intent.putExtra("image_name", mNames.get(position));
intent.putExtra("director_name", mDirector.get(position));
intent.putStringArrayListExtra("movieTrailer", mTrailer);
mContext.startActivity(intent);
}
});
}
private void setImage(String imageUrl, String imageName, String directorName, ArrayList trailerUrl) {
Log.d(TAG, "setImage: setting the image and name to widgets.");
TextView name = findViewById(R.id.text_movie_original_title);
name.setText(imageName);
ImageView image = findViewById(R.id.image_movie_detail_poster);
Glide.with(this)
.asBitmap()
.load(imageUrl)
.into(image);
TextView director = findViewById(R.id.text_movie_director_name);
director.setText(directorName);
RecyclerView trailer = findViewById(R.id.movie_videos);
trailer.set;
bundle.putParcelableArrayList("urlList", list)
send it via intent
get arraylist by
getParcelableArrayList("urlList")
i think these steps would help:
1. create arrays
put the same values to the arrays, you put on the recyclerView.
pass the arrays using intent. Example:
Bundle bundle = new Bundle();
bundle.putStringArray("arrayOfName", new String[]{name1, name2});
bundle.putStringArray("arrayOfImageUrls", new String[]{url1, url2});
Intent intent=new Intent(context, SecondActivity.Class);
intent.putExtras(bundle);
then in the second activity:
Bundle bundle = this.getIntent().getExtras();
String[] names=bundle.getStringArray("arrayOfName");
String[] urls=bundle.getStringArray("arrayOfImageUrls");
add this arrays to the recyclerView adapter in the SecondActivity
set the Adapter to your recyclerView
Related
Hello I want to have an Add function that allows me to input items to my GridView
For Background: I have a standard GridView and an XML activity (which contains 2 TextView) that I want to convert to my GridView. I also have a custom ArrayAdapter class and custom Word object (takes 2 Strings variables) that helps me do this.
My problem: I want to have an Add button that takes me to another XML-Layout/class and IDEALLY it input a single item and so when the user goes back to MainActivity the GridView would be updated along with the previous information that I currently hard-coded atm. This previous sentence doesn't work currently
Custom ArrayAdapter and 'WordFolder' is my custom String object that has 2 getters
//constructor - it takes the context and the list of words
WordAdapter(Context context, ArrayList<WordFolder> word){
super(context, 0, word);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.folder_view, parent, false);
}
//Getting the current word
WordFolder currentWord = getItem(position);
//making the 2 text view to match our word_folder.xml
TextView title = (TextView) listItemView.findViewById(R.id.title);
title.setText(currentWord.getTitle());
TextView desc = (TextView) listItemView.findViewById(R.id.desc);
desc.setText(currentWord.getTitleDesc());
return listItemView;
}
}
Here is my NewFolder code. Which sets contentview to a different XML. it's pretty empty since I'm lost on what to do
public class NewFolder extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_folder_view);
Button add = (Button) findViewById(R.id.add);
//If the user clicks the add button - it will save the contents to the Word Class
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//make TextView variables and cast the contents to a string and save it to a String variable
TextView name = (TextView) findViewById(R.id.new_folder);
String title = (String) name.getText();
TextView descText = (TextView) findViewById(R.id.desc);
String desc = (String) descText.getText();
//Save it to the Word class
ArrayList<WordFolder> word = new ArrayList<>();
word.add(new WordFolder(title, desc));
//goes back to the MainActivity
Intent intent = new Intent(NewFolder.this, MainActivity.class);
startActivity(intent);
}
});
}
In my WordFolder class I made some TextView variables and save the strings to my ArrayList<> object but so far it's been useless since it doesn't interact with the previous ArrayList<> in ActivityMain which makes sense because its an entirely new object. I thought about making the ArrayList a global variable which atm it doesn't make sense to me and I'm currently lost.
Sample code would be appreciative but looking for a sense of direction on what to do next. I can provide other code if necessary. Thank you
To pass data between Activities to need to do a few things:
First, when the user presses your "Add" button, you want to start the second activity in a way that allows it to return a result. this means, that instead of using startActivity you need to use startActivityForResult.
This method takes an intent and an int.
Use the same intent you used in startActivity.
The int should be a code that helps you identify where a result came from, when a result comes. For this, define some constant in your ActivityMain class:
private static final int ADD_RESULT_CODE = 123;
Now, your button's click listener should looks something like this:
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this, NewFolder.class);
startActivityForResult(intent, ADD_RESULT_CODE);
}
});
Now for returning the result.
First, you shouldn't go back to your main activity by starting another intent.
Instead, you should use finish() (which is a method defined in AppCompatActivity, you can use to finish your activity), this will return the user to the last place he was before this activity - ActivityMain.
And to return some data, too, you can use this code:
Intent intent=new Intent();
intent.putExtra("title",title);
intent.putExtra("desc",desc);
setResult(Activity.RESULT_OK, intent);
where title and desc are the variables you want to pass.
in your case it should look something like this:
public class NewFolder extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_folder_view);
Button add = (Button) findViewById(R.id.add);
//If the user clicks the add button - it will save the contents to the Word Class
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//make TextView variables and cast the contents to a string and save it to a String variable
TextView name = (TextView) findViewById(R.id.new_folder);
String title = (String) name.getText();
TextView descText = (TextView) findViewById(R.id.desc);
String desc = (String) descText.getText();
//Save it to the Word class
ArrayList<WordFolder> word = new ArrayList<>();
word.add(new WordFolder(title, desc));
Intent intent=new Intent();
intent.putExtra("title",title);
intent.putExtra("desc",desc);
setResult(Activity.RESULT_OK, intent);
//goes back to the MainActivity
finish();
}
});
}
You should probably also take care of the case where the user changed his mind and wants to cancel adding an item. in this case you should:
setResult(Activity.RESULT_CANCELLED);
finish();
In your ActivityMain you will have the result code, and if its Activity.RESULT_OK you'll know you should add a new item, but if its Activity.RESULT_CANCELLED you'll know that the user changed their mind
Now all that's left is receiving the data in ActivityMain, and doing whatever you want to do with it (like adding it to the grid view).
To do this you need to override a method called onActivityResult inside ActivityMain:
// Call Back method to get the Message form other Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check the result code to know where the result came from
//and check that the result code is OK
if(resultCode == Activity.RESULT_OK && requestCode == ADD_RESULT_CODE )
{
String title = data.getStringExtra("title");
String desc = data.getStringExtra("desc");
//... now, do whatever you want with these variables in ActivityMain.
}
}
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 currently following this tutorial to implement a shared element transition between view (cards) in RecyclerView and activity but I'm not sure how can I do it since I'm using an onClickListener on MyRecyclerAdapter class to start a new activity.
Just new in development, hope you can help me about it.
MyRecyclerAdapter.java
public class MyRecyclerAdapter extends RecyclerView.Adapter<PaletteViewHolder> {
private Context context;
private List<Palette> palettes;
public MyRecyclerAdapter(Context context, List<Palette> palettes) {
this.palettes = new ArrayList<Palette>();
this.palettes.addAll(palettes);
this.context = context;
}
#Override
public PaletteViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.card_view, viewGroup, false);
itemView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context, ScrollingActivity.class);
intent.putExtra("P25", "Longanissa");
context.startActivity(intent);
}
});
return new PaletteViewHolder(itemView);
}
#Override
public void onBindViewHolder(PaletteViewHolder paletteViewHolder, int i) {
Palette palette = palettes.get(i);
paletteViewHolder.titleText.setText(palette.getName());
paletteViewHolder.contentText.setText(palette.getHexValue());
paletteViewHolder.card.setCardBackgroundColor(palette.getIntValue());
}
#Override
public int getItemCount() {
return palettes.size();
}
animateIntent method:
public void animateIntent(View view) {
// Ordinary Intent for launching a new activity
Intent intent = new Intent(this, YourSecondActivity.class);
// Get the transition name from the string
String transitionName = getString(R.string.transition_string);
// Define the view that the animation will start from
View viewStart = findViewById(R.id.card_view);
ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(this,
viewStart, // Starting view
transitionName // The String
);
//Start the Intent
ActivityCompat.startActivity(this, intent, options.toBundle());
You need to set the transition name of the view inside the onBindViewHolder. It needs to be different of the others viewHolder so get a way to make it different and unique (using palette.getName() if palette names are unique for example or use i).
Then you need to start the activity inside on click using the provided view:
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(context, v, v.getTransitionName());
context.startActivity(intent, options.toBundle());
I don't understand why you need animateIntent method. Hope it still helps
Application Structure first:
//This class has a listview which will be populated from custom adapter
public class Transactions_show extends Activity implements OnItemClickListener
//This class is custom adapter which returns custom view for each row to be populated in above listview using sqlite database
public class CustomAdapter extends BaseAdapter
//Here all is working fine
//Now
//setting onitemclicklistener on each item of listview
public class Transactions_show extends ListActivity implements
OnItemClickListener {
List<Transactions> all_transactions;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_transactions);
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
all_transactions = db.get_all_transactions();
size_of_transaction = all_transactions.size();
ListView all_transactions_list = (ListView) findViewById(R.id.all_transaction_show_list);
CustomAdapter adapter = new CustomAdapter(this, all_transactions);
all_transactions_list.setAdapter(adapter);
try {
all_transactions_list.setOnItemClickListener(this);
} catch (NullPointerException e) {
Log.e("null pointer exception at item click", e.getMessage());
}
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Transactions single_transaction = new Transactions();
single_transaction = all_transactions.get(position);
Intent intent = new Intent(this, transacee_summary.class);
int p_n = single_transaction.get_phone_number();
Intent.putExtra("phone_number", p_n);
startActivity(intent);
/*
* Toast toast = Toast.makeText(getApplicationContext(), "Item " +
* (position + 1) + ": " +single_transaction.get_phone_number() ,
* Toast.LENGTH_SHORT); toast.setGravity(Gravity.BOTTOM |
* Gravity.CENTER_HORIZONTAL, 0, 0); toast.show();
*/
}
}
error is occuring here
Intent.putExtra("phone_number", p_n);
Which is : Cannot make a static reference to the non-static method putExtra(String, int) from the type Intent
Finding and trying on stack overflow for days and following google developers , decided to make a bundle as given in Passing a Bundle on startActivity()?
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Transactions single_transaction = new Transactions();
single_transaction = all_transactions.get(position);
Intent intent = new Intent(this, transacee_summary.class);
int p_n = single_transaction.get_phone_number();
Bundle extras = intent.getExtras();
try {
extras.putInt("phone_number", p_n);
} catch (NullPointerException e) {
Log.e("Null exception at putint", e.getMessage());
}
Intent.putExtra("phone_number", p_n);
startActivity(intent);
}
Error removed but when application runs but crashes at extras.puInt ,although next activity starts well if don't pass this bundle.
So thought about taking complete custom view row and extract field of phone number
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(this, transacee_summary.class);
TextView textv = (TextView) findViewById(R.id.phone_number_show);
int p_n = Integer.parseInt( textv.getText().toString());
Intent.putExtra("phone_number", p_n);
startActivity(intent);
}
now error comes again same.
What goes around comes around!
It should probably be:
intent.putExtra("phone_number", p_n);
intent (the instance), not Intent (the class), since putExtra is indeed an instance method.
Problem is with this piece of code:
Intent intent = new Intent(this, transacee_summary.class);
int p_n = single_transaction.get_phone_number();
Intent.putExtra("phone_number", p_n);
You need to replace Class Intent refrence with Object Intent refrence as putExtra is not a static method it is an instance method
Intent intent = new Intent(this, transacee_summary.class);
int p_n = single_transaction.get_phone_number();
intent.putExtra("phone_number", p_n); // see starts with small i
Hope this helps.
Ive searched for this all over the internet and there seems to be no simple explanation or tutorial on how to do this.
Basically, I want a layout that has a ListView where the user can click on an object and it will take them to the next layout.
In other words, using the listview as links to other layouts.
Everything Ive found on the internet has the end result of using a Toast... However I dont want a toast, i want to link to the next page.
Your question is slightly confusing so I'm going to make an assumption.
Is [LinearLayout1 LinearLayout2 Breadcrumb] suppose to be navigation or tabs that when selected insert their corresponding content into the Main Content?
If so I would suggest using fragments for each piece of content. Then when you click the navigation/tab, perform an animation of the fragment which slides the content in and out.
See the google docs for how to use fragments: http://developer.android.com/guide/components/fragments.html
See another stackoverflow answer for how to do the slide animation: Android Fragments and animation
or
http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html
Here is some code that outlines how to invoke an activity following a click on a list row. Hopefully you can adapt the Toast example you mention to make this work for you.
The basic idea is that you launch a new Activity with a new Intent. You can pass any data you need from the listView row as an extra in the Intent.
final static String[] months = new String[] {"Jan","Feb","Mar"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row_layout, R.id.text1, months);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int intItem = (int)id;
Intent intent= new Intent(this, SecondaryActivity.class);
intent.putExtra("MONTH", intItem);
startActivity(intent);
}
Why using ListView for it?
Each row must lead to different layout?
Its main benefits is in displaying dynamically changing data, but in your case data is constant, right?
Use vertical LinearLayout, fill it programmatically with "list elements", and add
leListComponent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(YourActivity.this, TargetActivity.class));
}
});
to each.
If i didn't get it, and you feel good of using some adapter, it can be like this:
public class LeWrapper {
private String caption;
private Class<? extends Activity> target;
...POJO here...
}
v.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//get leWrapper object from adapter
startActivity(new Intent(MenuActivity.this, leWrapper.getTarget()));
}
});
but its kinda overkill
Thanks for all the help guys. Sorry ive took my time replying. My solution is below :)
public class FP_WL1_ListView extends Activity {
private ListView lv1;
private String lv_arr[]={"Exercise Bike", "Treadmill", "Cross Trainer", "Squats", "Lunges"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fitnessprograms_wlday_one);
lv1=(ListView)findViewById(R.id.list);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final TextView mTextView = (TextView)view;
switch (position) {
case 0:
Intent newActivity0 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_sp.class);
startActivity(newActivity0);
break;
case 1:
Intent newActivity1 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Treadmill.class);
startActivity(newActivity1);
break;
case 2:
Intent newActivity2 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Crosstrainer.class);
startActivity(newActivity2);
break;
case 3:
Intent newActivity3 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Squats.class);
startActivity(newActivity3);
break;
case 4:
Intent newActivity4 = new Intent(FP_WL1_ListView.this,FitnessPrograms_Wlone_Lunges.class);
startActivity(newActivity4);
break;
}
}
});
} }