Binding data into activity - java

How to bind data into my activity, so that every item will be with correct text. I was following the tutorial but, it only shows how to load the image. Iwas trying with item.getTitle().into(titleorg); but got some errors
Thats my details activity
public class OrgDetails extends AppCompatActivity {
ImageView imgorg;
TextView titleorg, descorg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_org_details);
this.imgorg = findViewById(R.id.item_org_logo);
this.titleorg = findViewById(R.id.item_org_title);
this.descorg = findViewById(R.id.item_org_description);
Org item = (Org) getIntent().getExtras().getSerializable("orgObject");
loadOrgData(item);
}
private void loadOrgData(Org item) {
Glide.with(this).load(item.getDrawableResource()).into(imgorg);
}
Thats my model
public class Org implements Serializable {
private String title, description;
private int drawableResource;
public Org(int drawableResource) {
this.drawableResource = drawableResource;
}
public Org(String title, String description, int drawableResource) {
this.title = title;
this.description = description;
this.drawableResource = drawableResource;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getDrawableResource() {
return drawableResource;
}
public void setDrawableResource(int drawableResource) {
this.drawableResource = drawableResource;
}

Related

How to change the TextView from an object?

How can I use this to change the profile of a Turtle using Radio buttons? So when user selects a radio button different text should be displayed in the TextView. How can I set these objects? Currently there is no output displaying when the radio button is clicked. So I would like to get the details of the turtle from the object and set it.
//Main Activity
public class MainActivity extends AppCompatActivity {
private RadioGroup group_turtle;
private ImageView image_turtle;
private TextView text_description;
//create an array to store the turtle's profile
ArrayList<Turtle> turtles = new ArrayList<>(4);
//OnCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Load GUI components
loadGUI();
processRadioButtons();
loadTurtles();
}
private void loadTurtles() {
Turtle t1 = new Turtle("Leo","Leo is the heart of the team.",
"Loyal, brave, and responsible, Leonardo is the team leader, but not by choice.",
5);
Turtle t2 = new Turtle("Mike","Mikey is the jokester.",
"Slice of pizza in hand and shouts of \"COWABUNGA!\"",
4);
Turtle t3 = new Turtle("Don","Donny is the thinker.",
"Every team needs a brain, and for the Turtles, that's Donny. He's smart and philosophical.",
5);
Turtle t4 = new Turtle("Raphael","Raphael is fearless.",
"Raphael has the confidence and charisma of a \"D\" personality",
4);
//add the t1-t4 to the turtle list
turtles.add(t1);
turtles.add(t2);
turtles.add(t3);
turtles.add(t4);
/*try to display the turtle details in the logcat
for (Turtle t:turtles) {
Log.d("TURTLE", t.toString());
}*/
}
private void changeTurtle(int index) {
int images[] = {
R.drawable.tmntleo,
R.drawable.tmntmike,
R.drawable.tmntdon,
R.drawable.tmntraph};
//change the turtle image using setImageresource
image_turtle.setImageResource(images[index]);
//change the turtle profile
//I'm trying to change the Turtle's text description in here which is the textview.
String t1 = text_description.getText().toString();
text_description.setText(t1);
}
private void processRadioButtons() {
//set the listener
group_turtle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup radioGroup, int id) {
switch (id) {
case R.id.radio_leo:
MainActivity.this.changeTurtle(0);
break;
case R.id.radio_mike:
MainActivity.this.changeTurtle(1);
break;
case R.id.radio_don:
MainActivity.this.changeTurtle(2);
break;
case R.id.radio_raph:
MainActivity.this.changeTurtle(3);
break;
}
}
});
}
private void loadGUI() {
group_turtle = findViewById(R.id.radio_group);
image_turtle = findViewById(R.id.imageView);
text_description = findViewById(R.id.text_description);
}
}
package com.example.ninjaturtle;
// new Turtle class
public class Turtle {
private String name;
private String features;
private String description;
private int rating;
//constructor
/*public Turtle(String name, String features, String description, int rating) {
this.name = name;
this.features = features;
this.description = description;
this.rating = rating;
}*/
//setter & getter
/*public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFeatures() {
return features;
}
public void setFeatures(String features) {
this.features = features;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
//toString method
#Override
public String toString() {
return "Turtle{" +
"name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}*/
}
Change String t1 = text_description.getText().toString();
to
String desc= turtles.get(index).getDescription();
or to print all data
String desc= turtles.get(index).toString();
in your changeTurtle() method

How to create a retrofit2 array in android studio?

I took this information out of a JSON and initialized it, but I don't understand how I can get the data out of the form to pass them to the cardView afterward. Or is there an easier way?
MainActivity
public class MainActivity extends AppCompatActivity {
Context context;
Example example;
Response responses;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView rv = (RecyclerView)findViewById(R.id.cartView);
LinearLayoutManager llm = new LinearLayoutManager(context);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("Secret Url") // For some reason I cannot show the url
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
ArrayList arrayList = new ArrayList();
Call<Example> call = api.getRespon();
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
if(response.code() != 200){
Toast.makeText(MainActivity.this, "Connect",Toast.LENGTH_SHORT).show();
return;
}
Example examples = response.body();
for(int i=0; i<examples.getResponse().size(); i++){
String f_name = response.body().getResponse().iterator().next().getfName();
String l_name = response.body().getResponse().iterator().next().getlName();
String birthday = response.body().getResponse().iterator().next().getBirthday();
String SpecName = response.body().getResponse().iterator().next().getSpecialty()
.iterator().next().getName();
int SpecId = response.body().getResponse().iterator().next().getSpecialty()
.iterator().next().getSpecialtyId();
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
Toast.makeText(MainActivity.this, "Fail",Toast.LENGTH_SHORT).show();
}
});
}
}
At the moment I have not finished the class because I can not specify the data that have been implemented
PersonaAdapter
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.PersonViewHolder> {
Context context;
Example example;
MainActivity mainActivity;
public static class PersonViewHolder extends RecyclerView.ViewHolder{
CardView cv;
TextView l_name, f_name, birthday, SpecName, Age;
ImageView avatar;
public PersonViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.cartView);
l_name = itemView.findViewById(R.id.textLName);
f_name = itemView.findViewById(R.id.textFName);
birthday = itemView.findViewById(R.id.textBirthday);
SpecName = itemView.findViewById(R.id.textSpecName);
Age = itemView.findViewById(R.id.textAge);
}
}
// иницилизация
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,viewGroup,false);
PersonViewHolder phv = new PersonViewHolder(v);
return phv;
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
//***
}
#Override
public int getItemCount() {
return example.getResponse().size();
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
Handler itself
Response
public class Response {
#SerializedName("f_name")
#Expose
private String fName;
#SerializedName("l_name")
#Expose
private String lName;
#SerializedName("birthday")
#Expose
private String birthday;
#SerializedName("avatr_url")
#Expose
private String avatrUrl;
#SerializedName("specialty")
#Expose
private List<Specialty> specialty = null;
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public Response withfName(String fName) {
this.fName = fName;
return this;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Response withlName(String lName) {
this.lName = lName;
return this;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Response withBirthday(String birthday) {
this.birthday = birthday;
return this;
}
public String getAvatrUrl() {
return avatrUrl;
}
public void setAvatrUrl(String avatrUrl) {
this.avatrUrl = avatrUrl;
}
public Response withAvatrUrl(String avatrUrl) {
this.avatrUrl = avatrUrl;
return this;
}
public List<Specialty> getSpecialty() {
return specialty;
}
public void setSpecialty(List<Specialty> specialty) {
this.specialty = specialty;
}
public Response withSpecialty(List<Specialty> specialty) {
this.specialty = specialty;
return this;
}
}
Specialty
public class Specialty {
#SerializedName("specialty_id")
#Expose
private Integer specialtyId;
#SerializedName("name")
#Expose
private String name;
public Integer getSpecialtyId() {
return specialtyId;
}
public void setSpecialtyId(Integer specialtyId) {
this.specialtyId = specialtyId;
}
public Specialty withSpecialtyId(Integer specialtyId) {
this.specialtyId = specialtyId;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Specialty withName(String name) {
this.name = name;
return this;
}
}
I solved the problem by creating a list and putting the data into an array
List<Worker> workerList = new ArrayList<>();
for(int i = 0; i < examples.getResponse().size(); i++){
String f_name = response.body().getResponse().get(i).getfName();
String l_name = response.body().getResponse().get(i).getlName();
String birthday = response.body().getResponse().get(i).getBirthday();
String SpecName = response.body().getResponse().get(i).getSpecialty()
.iterator().next().getName();
String SpecId = response.body().getResponse().iterator().next().getSpecialty()
.iterator().next().getSpecialtyId().toString();
String AvatarUrl = response.body().getResponse().get(i).getAvatrUrl();
workerList.add(new Worker(f_name, l_name, birthday, SpecName, SpecId, AvatarUrl));
In Worker has a constructor and getter

URL based Movie Streaming App , need help in passing variable strings from one class to another

I would like to call the value of the String streamingLink to
String path1 ="xxxxx"; on MoviePlayerActivity.class (mentioned below)
Instead of xxxx, I need it to be the movie URL
Also, each movie has different URL stored inside an Array of List in DataSources.class
public class Movie {
private String title;
private String description;
private int thumbnail;
private int coverPhoto;
private String imdb;
private String rt;
private String streamingLink;
//Cast Initializing
private int cast1;
private int cast2;
private int cast3;
private String actor1;
private String actor2;
private String actor3;
public Movie(String title, int thumbnail, int coverPhoto, String actor1, int cast1, String actor2, int cast2, String actor3, int cast3, String streamingLink, String imdb, String rt, String description) {
this.title = title;
this.thumbnail = thumbnail;
this.coverPhoto = coverPhoto;
this.description =description;
this.imdb = imdb;
this.rt = rt;
this.streamingLink = streamingLink;
// Cast
this.cast1 = cast1;
this.cast2 = cast2;
this.cast3 = cast3;
this.actor1 = actor1;
this.actor2 = actor2;
this.actor3 = actor3;
}
public int getCoverPhoto() {
return coverPhoto;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public int getThumbnail() {
return thumbnail;
}
public String getImdb() {
return imdb;
}
public String getRt() {
return rt;
}
//Cast get activity
public int getCast1() {
return cast1;
}
public int getCast2() {
return cast2;
}
public int getCast3() {
return cast3;
}
public String getActor1() {
return actor1;
}
public String getActor2() {
return actor2;
}
public String getActor3() {
return actor3;
}
public String getStreamingLink() {
return streamingLink;
}
public void setTitle(String title) {
this.title = title;
}
public void setCoverPhoto(int coverPhoto) {
this.coverPhoto = coverPhoto;
}
public void setDescription(String description) {
this.description = description;
}
public void setThumbnail(int thumbnail) {
this.thumbnail = thumbnail;
}
public void setImdb(String imdb) {
this.imdb = imdb;
}
public void setRt(String rt) {
this.rt = rt;
}
public void setStreamingLink(String streamingLink) {
this.streamingLink = streamingLink;
}
//Cast set activity
public void setCast1(int cast1) {
this.cast1 = cast1;
}
public void setActor1(String actor1) {
this.actor1 = actor1;
}
public void setCast2(int cast2) {
this.cast2 = cast2;
}
public void setActor2(String actor2) {
this.actor2 = actor2;
}
public void setCast3(int cast3) {
this.cast3 = cast3;
}
public void setActor3(String actor3) {
this.actor3 = actor3;
}
}
MoviePlayerActivity:
public class MoviePlayerActivity extends AppCompatActivity {
private PlayerView playerView;
FloatingActionButton play_fab;
private SimpleExoPlayer simpleExoPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setFullScreen();
setContentView( R.layout.activity_movie_player );
hideActionbar();
iniExoPlayer();
}
private void hideActionbar() {
getSupportActionBar().hide();
}
private void setFullScreen() {
requestWindowFeature( Window.FEATURE_NO_TITLE );
getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
}
private void iniExoPlayer() {
playerView=findViewById( R.id.movie_exo_player );
simpleExoPlayer = ExoPlayerFactory.newSimpleInstance( this );
playerView.setPlayer( simpleExoPlayer );
String path1 ="xxxxx";
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory( this,
Util.getUserAgent(this,"appname"));
MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory )
.createMediaSource( Uri.parse( path1) );
simpleExoPlayer.prepare( videoSource );
simpleExoPlayer.setPlayWhenReady( true );
}
#Override
protected void onDestroy() {
super.onDestroy();
simpleExoPlayer.release();
}
}
Here's how the array looks like
this is class DataSource
public static List getEnglishMovies(){
List<Movie> lstMovies = new ArrayList<>();
lstMovies.add( new Movie( "Avengers: Endgame" , R.drawable.avgendgame, R.drawable.avgendgamecp,"Chris Hemsworth",R.drawable.chrishemsworth,"Robert Downey Jr.",R.drawable.downeyjr,"Chris Hemswoth",R.drawable.chrishemsworth,"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4","8.4","NA","After the devastating events of Avengers: Infinity War (2018), the universe is in ruins. With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos' actions and restore balance to the universe.") );
lstMovies.add( new Movie( "Extraction" ,R.drawable.extraction, R.drawable.extractioncp,"Chris Hemswoth",R.drawable.chrishemsworth,"Chris Hemswoth",R.drawable.chrishemsworth,"Chris Hemswoth",R.drawable.chrishemsworth,"http://168.61.51.210/movies/Extraction.mp4","6.8","68%","Tyler Rake, a fearless black market mercenary, embarks on the most deadly extraction of his career when he's enlisted to rescue the kidnapped son of an imprisoned international crime lord.") );
lstMovies.add( new Movie( "The Call Of The Wild" ,R.drawable.thecallofthewildjpg,R.drawable.thecallofthewildcp,"Chris Hemswoth",R.drawable.chrishemsworth,"Chris Hemswoth",R.drawable.chrishemsworth,"Chris Hemswoth",R.drawable.chrishemsworth,"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4","6.8","62%", "Adapted from the beloved literary classic, THE CALL OF THE WILD vividly brings to the screen the story of Buck, a big-hearted dog whose blissful domestic life is turned upside down when he is suddenly uprooted from his California home and transplanted to the exotic wilds of the Alaskan Yukon during the Gold Rush of the 1890s. As the newest rookie on a mail delivery dog sled team--and later its leader--Buck experiences the adventure of a lifetime, ultimately finding his true place in the world and becoming his own master.") );
lstMovies.add( new Movie( "Do Little" ,R.drawable.dolittle, R.drawable.dolittlecp,"Chris Hemswoth",R.drawable.chrishemsworth,"Chris Hemswoth",R.drawable.chrishemsworth,"Chris Hemswoth",R.drawable.chrishemsworth,"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4","5.6","NA","A physician who can talk to animals embarks on an adventure to find a legendary island with a young apprentice and a crew of strange pets.") );
return lstMovies;
}

RecyclerView, wrong positioning of data

im making a program that can view data and image at the same time using recyclerview
MainActivity
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ArrayList<Upload> mUpload = new ArrayList<>();
String y = "report";
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
childcount = postSnapshot.child(userID).getChildrenCount() - 5;
String z = y+childcount;
Upload upload = postSnapshot.child(userID).child(z).getValue(Upload.class);
Upload uploads = postSnapshot.child(userID).getValue(Upload.class);
mUploads.add(upload);
mUploads.add(uploads);
mUpload.addAll(mUploads);
Log.d(TAG, "sampleData " + mUploads);
}
mAdapter = new ImageAdapter(getActivity(), mUpload);
mRecycleView.setAdapter(mAdapter);
}
ImageAdapter
#Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getiName());
holder.textViewDate.setText(uploadCurrent.getDate());
holder.textViewAddress.setText(uploadCurrent.getAddress());
holder.textViewPhone.setText(uploadCurrent.getPhone());
holder.textViewReport.setText(uploadCurrent.getReport());
Picasso.with(mContext)
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
holder.setIsRecyclable(false);
holder.getLayoutPosition();
}
Upload Class
public class Upload {
private String mName;
private String mImageUrl;
private String date;
private String address;
private String phone;
private String iName;
private String report;
public Upload() {
//empty constructor needed
}
public Upload(String name, String imageUrl) {
if (name.trim().equals("")) {
name = "No Name";
}
mName = name;
mImageUrl = imageUrl;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getiName() {
return iName;
}
public void setiName(String iName) {
this.iName = iName;
}
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public String getDate() { return date; }
public void setDate(String date) { this.date = date; }
public String getName() {
return mName;
}
public void setName(String name) {
mName = name;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}
xml file
enter image description here
output i get
enter image description here
as you can see i got wrong positioning of data, i also try to merge the 2 arraylist data which is upload and uploads but nothings change, can someone help me with this project. thank you so much

How to pass a class with an abstract class type as parameter in parcelable android studio java

I am trying to pass this class as a parcelable in android.
public class Outfit implements Parcelable {
private List<Item> itemList;
private String mName;
private String mImageUrl;
public Outfit() {}
public Outfit(String mName, String mImageUrl) {
this.mName = mName;
this.mImageUrl = mImageUrl;
}
protected Outfit(Parcel in) {
itemList = in.createTypedArrayList(Item.CREATOR);
mName = in.readString();
mImageUrl = in.readString();
}
public static final Creator<Outfit> CREATOR = new Creator<Outfit>() {
#Override
public Outfit createFromParcel(Parcel in) {
return new Outfit(in);
}
#Override
public Outfit[] newArray(int size) {
return new Outfit[size];
}
};
public String getmName() {
return mName;
}
public String getmImageUrl() {
return mImageUrl;
}
#Override
public String toString() {
return mName;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeTypedList(itemList);
parcel.writeString(mName);
parcel.writeString(mImageUrl);
}
}
the problem is that Item is abstract and itemList = in.creatTypedArrayList(Item.CREATOR) because Item does not have a CREATOR . Only its subclasses have this implementation.
Item.java
public abstract class Item implements Parcelable {
private String mName;
private String mColor;
private String mImageUrl;
private List<TagHolder> tags = new ArrayList<>();
private String mKey;
public Item(){
}
public Item(String mName, String mColor, String mImageUrl) {
this.mName = mName;
this.mColor = mColor;
this.mImageUrl = mImageUrl;
}
protected Item(Parcel in) {
mName = in.readString();
mColor = in.readString();
mImageUrl = in.readString();
tags = in.createTypedArrayList(TagHolder.CREATOR);
mKey = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeString(mColor);
dest.writeString(mImageUrl);
dest.writeTypedList(tags);
dest.writeString(mKey);
}
#Override
public int describeContents() {
return 0;
}
public void setmName(String mName) {
this.mName = mName;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
public List<TagHolder> getTags() {
return tags;
}
public String getmColor() {
return mColor;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmColor(String mColor) {
this.mColor = mColor;
}
public String getmName() {
return mName;
}
public void setTags(List<TagHolder> tags) {
this.tags = tags;
}
#Exclude // dont need this in our firebase database
public String getKey() {
return mKey;
}
#Exclude
public void setMkey(String key) {
mKey = key;
}
public abstract String getCategory();
}
I am able to parce a List as a parcelable array when I call put extra. But when I try to do so for outfit it gives error . Is there any way to pass Outfit as a parcelable?
Do you try using another method for the list?
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeList(itemList);
parcel.writeString(mName);
parcel.writeString(mImageUrl);
}
protected Outfit(Parcel in) {
in.readList(itemList, Item.class.getClassLoader());
mName = in.readString();
mImageUrl = in.readString();
}

Categories

Resources