codepath / android_guides

Extensive Open-Source Guides for Android Developers
guides.codepath.com
MIT License
28.29k stars 6.35k forks source link

User in RecyclerView #299

Open bigboisaybj opened 7 years ago

bigboisaybj commented 7 years ago

Hey,

I'm new to Android and love how in-depth these tutorials are compared to a lot of the other material online. However, I'm confused at where User and RecyclerViewSimpleTextViewHolder come from in the 'Heterogenous Layouts inside RecyclerView'.

private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
        vh.getLabel().setText((CharSequence) items. get(position));   

private void configureViewHolder1(ViewHolder1 vh1, int position) {
        User user = (User) items.get(position);
        if (user != null) {
            vh1.getLabel1().setText("Name: " + user.name);
            vh1.getLabel2().setText("Hometown: " + user.hometown);
        }
    }`

Thanks!

vivonk commented 7 years ago

Ok I get you, actually user is a class which is for creating recycler view components, in other words it's called Adapter Object class. And second, RecyclerViewSimpleTextViewHolder is another class like this

 package uk.co.example.calculator.adapters;

 import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import uk.co.example.calculator.R; 
import uk.co.example.calculator.ui.activities.Currency;

public class SimpleItemRecyclerViewAdapter extends    
RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.RecyclerViewSimpleTextViewHolder> {

// The items to display in your RecyclerView
private List<Currency> items;

// Provide a suitable constructor (depends on the kind of dataset)
public SimpleItemRecyclerViewAdapter(List<Currency> items) {
    this.items = items;
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return this.items.size();
}

// Replace the contents of a view (invoked by the layout manager)
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(RecyclerViewSimpleTextViewHolder holder, int position) {
    Currency currency = items.get(position);
    holder.getName().setText(currency.name);
    holder.getSymbol().setText(currency.symbol);
    holder.getAsset().?????????; // WHAT SHOULD I PUT HERE TO LOAD THE IMAGE FROM THE RAW FOLDER
}

// Create new items (invoked by the layout manager)
// Usually involves inflating a layout from XML and returning the holder
@Override
public RecyclerViewSimpleTextViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).
            inflate(R.layout.currency_list_card_layout, viewGroup, false);
    return new RecyclerViewSimpleTextViewHolder(itemView);
}

public class RecyclerViewSimpleTextViewHolder extends RecyclerView.ViewHolder {
    private TextView name;
    private TextView symbol;
    private ImageView asset;

    public RecyclerViewSimpleTextViewHolder(View v) {
        super(v);
        name = (TextView) v.findViewById(R.id.currency_name);
        symbol = (TextView) v.findViewById(R.id.currency_symbol);
        asset = (ImageView) v.findViewById(R.id.currency_asset);
    }

    public TextView getName() {
        return name;
    }

    public TextView getSymbol() {
        return symbol;
    }

    public ImageView getAsset() {
        return asset;
    }

     }
 } 
torryola commented 3 years ago

Firstly, holder.getAsset() points to imageView, so the appropriate name should have been holder.getImageView(). Answer to your question: You need to load the image from asset folder as follows: InputStream stream = new AssetManager().open("Path to YourFileName"); // Note if the image file is in asset sub directory then includenit e.g. imageFolder/myImage.jpg otherwise just myImage.jpg // To Bitmap Bitmap myImage = BitmapFactory.decodeStream(stream); // Set the bitmmap holder.getAsset().setImageBitmap(myImage);