How to get the data from .txt files in Android - java

I am making an app where I have uploaded text files in Firebase Storage and now I want to retrieve those files and display the contents of those txt files in a text view inside a recycler view. I am able to get the uri of those text files but how do I display the contents of those files.
Here is the code I have done so far
public class RecyclerViewAdapter3 extends RecyclerView.Adapter<RecyclerViewAdapter3.ViewHolder> {
Context context3;
List<ImageUploadInfo> MainImageUploadInfoList;
public RecyclerViewAdapter3(Context context, List<ImageUploadInfo> TempList) {
this.MainImageUploadInfoList = TempList;
this.context3 = context;
}
public RecyclerViewAdapter3.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items3, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
#Override
public void onBindViewHolder(final RecyclerViewAdapter3.ViewHolder holder, int position) {
ImageUploadInfo UploadInfo = MainImageUploadInfoList.get(position);
Uri uri = Uri.parse(UploadInfo.getImageURL());
Log.d("TAGGG" , ""+uri);
holder.userText.setText(uri.toString());
holder.imageNameTextView.setText(UploadInfo.getImageName());
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView imageNameTextView ;
public TextView userText;
public ViewHolder(View itemView) {
super(itemView);
userText = (TextView) itemView.findViewById(R.id.textView13);
imageNameTextView = (TextView) itemView.findViewById(R.id.caption);
}
}
}
In this code I am setting the uri in the textview. How do I set the content of the text file into the textview?
Please help

Change your file to JSON format.
{
"data" : "your text here"
}
And use common approach to download data and parse json (I suggest Retrofit).

Related

Why my images is not showing from firebase firestore?

I am working on an application where i am uploading multiple images with 4 edittexts to the firebase then I am trying to retrieve the data to the recyclerview now my editext's data is retrieving but the data of images is not showing in reyclerview.
my adapter code
#Override
public void onBindViewHolder(#NonNull Viewholder holder, int position)
{
holder.name.setText(datalist.get(position).getName());
holder.email.setText(datalist.get(position).getEmail());
holder.desc.setText(datalist.get(position).getDesc());
holder.book.setText(datalist.get(position).getBook());
Glide.with(context).load(datalist.get(position).getImage())
.into(holder.imageView);
}
#Override
public int getItemCount() {
return datalist.size();
}
public class Viewholder extends RecyclerView.ViewHolder{
TextView name, email, desc, book;
ImageView imageView;
public Viewholder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.text_name);
email = itemView.findViewById(R.id.text_email);
desc = itemView.findViewById(R.id.text_desc);
book = itemView.findViewById(R.id.text_book);
imageView = itemView.findViewById(R.id.imageView3);
}
}
In the comment session you mentioned, getImage() is an array then, How it will load the image you need to pass a particular image in the array?
Let's suppose, If you want to load the first image in an array use the below code -
if(!wishlist_models.get(position).getImage().isEmpty()) {
Glide.with(context)
.load(wishlist_models.get(position).getImage()get(0))
.into(holder.wishlist_image);
}

Displaying User Images to CardView from Firebase using Picasso Not Working

I am trying to display images from my Firebase storage and realtime database to a CardView in my Android application. I have saved the images to the Firebase Storage and as a Url node under the specific user (dog). The CardView displays the dogs registered under each owner and each dog has its own profile picture essentially.. which I would like to display on the UI.
I am using Picasso as it was used within the tutorial I found, and seemed to be working, however it is trying to find 'context' within my DogUserAdapter, which is returning null. I wasn't previously using Context context as I am utilising a callback function instead on this particular CardView. What else can I use here to display the image from the database?
DogUserAdapter
public class DogUserAdapter extends FirebaseRecyclerAdapter<Dog, DogUserAdapter.DogViewHolder> {
private DogCallback dogCallback;
private Context context;
public DogUserAdapter(#NonNull FirebaseRecyclerOptions<Dog> options, #NonNull final DogCallback dogCallback) {
super(options);
this.dogCallback = dogCallback;
this.context = context;
}
#Override
protected void onBindViewHolder(#NonNull DogViewHolder holder, int position, #NonNull Dog model) {
holder.dogName.setText(model.getName());
Picasso.with(context)
.load(model.getImageUrl())
.into(holder.dogImage);
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dogCallback.onCardViewClick(model);
}
});
}
#NonNull
#Override
public DogViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dog, parent, false);
return new DogUserAdapter.DogViewHolder(view);
}
public class DogViewHolder extends RecyclerView.ViewHolder {
TextView dogName;
ImageView dogImage;
CardView cardView;
public DogViewHolder(#NonNull View itemView) {
super(itemView);
dogName = itemView.findViewById(R.id.dogName);
dogImage = itemView.findViewById(R.id.dogImage);
cardView = itemView.findViewById(R.id.card_view_dog);
}
}
public interface DogCallback{
void onCardViewClick(final Dog dog);
}
}
CardView
Firebase Hierarchy
Patrick is right. You do not need context for picasso
Picasso.get().load(url).into(imageView);

How to change an image in RecyclerView locally using drawable

I'm trying to change the image in a RecyclerView with a local drawable image. So in the project the RecyclerView is getting data from a local database, when a boolean column gets true, it should change into a tick. My item view looks like this.
And when the column is true, that clock symbol changes to tick. Here is the code which I tried:
My Adapter code:
public class HawbListAdapter extends RecyclerView.Adapter<HawbListAdapter.MyViewHolder> {
private Context context;
private ArrayList<HawbLists> hawbLists;
private DatabaseHelper mDatabaseHelper;
public HawbListAdapter(Context context, ArrayList<HawbLists> hawbLists) {
this.context = context;
this.hawbLists = hawbLists;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.hawb_list, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.Hcode.setText(hawbLists.get(position).getHawbCode());
holder.name.setText(hawbLists.get(position).getName());
if (hawbLists.get(position).isTick()) {
holder.timeImage.setImageResource(R.drawable.ic_right);
}
//Picasso.get().load(hawbLists.get(position).isTick()).into(holder.timeImage);
//holder.timeImage.setImageResource(R.drawable.ic_right);
}
#Override
public int getItemCount() {
return hawbLists.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView Hcode;
private TextView name;
private ImageView timeImage;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
Hcode = itemView.findViewById(R.id.hawbCode12345);
name = itemView.findViewById(R.id.name_hawb_list);
timeImage = itemView.findViewById(R.id.timeImage);
}
}
}
even when I'm using this
if (hawbLists.get(position).isTick()) {
holder.timeImage.setImageResource(R.drawable.ic_right);
}
and one of the items is true, it is still showing the same clock (wait) symbol, but If I use this:
if (!hawbLists.get(position).isTick()) {
holder.timeImage.setImageResource(R.drawable.ic_right);
}
then all of them regardless of true or false turn into tick mark. Unable to figure out where I'm doing it wrong.
Try this:
if (hawbLists.get(position).isTick()){
holder.timeImage.setImageResource(R.drawable.ic_right);
} else {
holder.timeImage.setImageResource(R.drawable.ic_not_ticked);
}
Do you know about the concept of recycling? Look at your list in the device and see how many rows you have! Now add up one (as swap view) and it's all the views you have. It means no matter how many entries you have (hawbLists.size()), all of them are created/recycled when you scroll. So you have to consider all the possible cases of your item children. Because otherwise a recycled view won't change the view state and likely will show irrelevant behavior.

How to open Separate url in Browser for each item in Recycle view?

I'm using Recycleview with Cardview in Gridlayout for Books app which will be fetched from Json . I'm looking to open different url which will download a separate books on for each item. How do I create this...??
Below is my adapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Model> mDataset;
private ImageLoader mImageLoader;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView nameText;
public TextView phoneText;
public NetworkImageView image;
#SuppressLint("WrongViewCast")
public ViewHolder(View v) {
super(v);
nameText = v.findViewById(R.id.name_text);
phoneText = v.findViewById(R.id.phone_text);
image = v.findViewById(R.id.imgAvatar);
image.setDefaultImageResId(R.mipmap.ic_launcher);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Position: "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
});
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Model> myDataset, Context mCOntext) {
Log.d("TEST",myDataset.get(0).getName());
mDataset = myDataset;
mImageLoader = MySingleton.getInstance(mCOntext).getImageLoader();
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
//holder.mTextView.setText(mDataset.get(position).getName());
Log.d("TEST","Printing Names onBindView Holder"+mDataset.get(position).getName());
holder.nameText.setText(mDataset.get(position).getName());
holder.phoneText.setText(mDataset.get(position).getPhone());
holder.image.setImageUrl(mDataset.get(position).getImage(),mImageLoader);
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mDataset.size();
}
}
Below is the screenshot of my Grid layout
Thanks in advance.
Do you have an URL for each Book ? If so, you can store it in your Model. Then, you can store it in the ViewHolder and retrieve it in the OnClickListener.
It means
Model.java
public class Model {
// your model
private String mUrl;
String getUrl() {
return mUrl;
}
}
MyAdapter.java
public static class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView nameText;
public TextView phoneText;
public NetworkImageView image;
public String url;
#SuppressLint("WrongViewCast")
public ViewHolder(View v) {
super(v);
nameText = v.findViewById(R.id.name_text);
phoneText = v.findViewById(R.id.phone_text);
image = v.findViewById(R.id.imgAvatar);
image.setDefaultImageResId(R.mipmap.ic_launcher);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(),"Position: "+getAdapterPosition(), Toast.LENGTH_SHORT).show();
// you can use url here
}
});
}
}
// ...
// Replace the contents of a view (invoked by the layout manager)
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
Model model = mDataset.get(position);
// - replace the contents of the view with that element
//holder.mTextView.setText(model.getName());
Log.d("TEST","Printing Names onBindView Holder"+mDataset.get(position).getName());
holder.nameText.setText(model.getName());
holder.phoneText.setText(model.getPhone());
holder.image.setImageUrl(model.getImage(),mImageLoader);
holder.url = model.getUrl();
}

Calling activity from ViewHolder in RecyclerView?

I've got a RecyclerView which is loading the content of my String arrays which works fine, however I want to open a new activity depending on which view they have pressed.
What I have done is created an array called classes like so:
<array name="classes">
<item>ClassOne</item>
<item>ClassTwo</item>
<item>ClassThree</item>
<item>ClassFour</item>
</array>
These are stored in an array, and passed to my MainActivityList adapter below:
String[] classes = resource.getStringArray(R.array.classes);
MainActivityList adapter = new MainActivityList(titles,content, images, classes);
recyclerView.setAdapter(adapter);
I've managed to add the OnClickListener to the ViewHolder and output what class is assigned to each view to the log, however I cannot figure out or get working, how to launch another activity.
The class name would be something like ClassOne.class for example
public class MainActivityList extends RecyclerView.Adapter<MainActivityList.ViewHolder> {
private String[] mTitles;
private String[] mContent;
private String[] mClasses;
private TypedArray mImages;
private Context context;
public MainActivityList(String[] titles, String[] content, TypedArray images, String[] classes) {
this.mTitles = titles;
this.mContent = content;
this.mImages = images;
this.mClasses = classes;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup,int i) {
final View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main_card, viewGroup, false);
ViewHolder vh = new ViewHolder(v);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ViewHolder vh = (ViewHolder)v.getTag();
Log.v("DEBUG", "Clicked" + vh.classes);
}
});
return vh;
}
public void onBindViewHolder(ViewHolder holder, int position) {
holder.titleView.setText(mTitles[position]);
holder.contentView.setText(mContent[position]);
holder.imageView.setImageDrawable(mImages.getDrawable(position));
holder.classes = mClasses[position];
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleView;
public TextView contentView;
public ImageView imageView;
public String classes;
public ViewHolder(View v) {
super(v);
titleView = (TextView) v.findViewById(R.id.card_title);
contentView = (TextView) v.findViewById(R.id.card_content);
imageView = (ImageView)v.findViewById(R.id.card_image);
v.setTag(this);
}
}
#Override
public int getItemCount() {
return mTitles.length;
}
}
Intent intent = new Intent(viewObject.getContext(),ActivityName.class);
viewObject.getContext().startActivity(intent);
I was also looking for a solution, and I found this post:
http://venomvendor.blogspot.sg/2014/07/setting-onitemclicklistener-for-recycler-view.html
By using the OnItemClickListener interface, you should be able to call startActivity in your activity.
adapter.SetOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(View v , int position) {
// This is in an Activity so should be able to start new activity, etc.
}
});
Update: I have tested, the method mentioned in the blog worked for me.

Categories