-1

i am getting error of context. it is showing getcontext can't be resolved in my adapter class. I am creating a earthquake report app like the one in udacity courses. i added recycler view and card view.therefore i made an adapter class. when i started to adding color to my magnitude text views i created a method of get magnitude color method in which i have to take out color from colors.xml file but it requires context and it is saying context cant be resolved . i am providing the code of my app...plz help me i'm new to android development

my adapter class code:-

package com.example.earthquakereport;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class myAdapter extends RecyclerView.Adapter<myAdapter.myViewHolder>{
    ArrayList<DataModel> dataModels;
    private static final String LOCATION_SEPARATOR = " of ";
    String primaryLocation,secondaryLocation;
    public myAdapter(ArrayList<DataModel> dataModels) {
    this.dataModels = dataModels;

}


    @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row,parent,false);
        return new myViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull myViewHolder holder, int position) {

//        holder.mag.setText(dataModels.get(position).getMag());
        holder.location.setText(dataModels.get(position).getLocation());
        Date dateObject = new Date(dataModels.get(position).getTimeInMilliseconds());
        String formattedDate = formatDate(dateObject);
        holder.date.setText(formattedDate);
        String formattedTime = formatTime(dateObject);
        holder.time.setText(formattedTime);

        String originalLocation = dataModels.get(position).getLocation();
        if(originalLocation.contains(LOCATION_SEPARATOR)){
            String[] parts = originalLocation.split(LOCATION_SEPARATOR);
            primaryLocation = parts[0];
            secondaryLocation = parts[1];
        }
//        else{
//            primaryLocation = context.getString(R.string.near_the);
//            secondaryLocation = originalLocation;
//        }
        holder.location.setText(primaryLocation);
        holder.location2.setText(secondaryLocation);


        String formattedMagnitude = formatMagnitude(dataModels.get(position).getMag());
        holder.mag.setText(formattedMagnitude);
        GradientDrawable magnitudeCircle = (GradientDrawable) holder.mag.getBackground();

        // Get the appropriate background color based on the current earthquake magnitude
        int magnitudeColor = getMagnitudeColor(dataModels.get(position).getMag());

        // Set the color on the magnitude circle
        magnitudeCircle.setColor(magnitudeColor);



    }

    @Override
    public int getItemCount() {
        return dataModels.size();
    }


    public class myViewHolder extends RecyclerView.ViewHolder {
        TextView mag,location,location2,date,time;


        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            mag = itemView.findViewById(R.id.mag_textview);
            location = itemView.findViewById(R.id.location_textview);
            location2 = itemView.findViewById(R.id.location2_textview);
            date = itemView.findViewById(R.id.date_textview);
            time = itemView.findViewById(R.id.time_textview);


        }
    }
    private String formatDate(Date dateObject) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("LLL dd, yyyy");
        return dateFormat.format(dateObject);
    }

    /**
     * Return the formatted date string (i.e. "4:30 PM") from a Date object.
     */
    private String formatTime(Date dateObject) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a");
        return timeFormat.format(dateObject);
    }

    private String formatMagnitude(double magnitude) {
        DecimalFormat magnitudeFormat = new DecimalFormat("0.0");
        return magnitudeFormat.format(magnitude);
    }
    private int getMagnitudeColor(double magnitude) {
        int magnitudeColorResourceId;
        int magnitudeFloor = (int) Math.floor(magnitude);
        switch (magnitudeFloor) {
            case 0:
            case 1:
                magnitudeColorResourceId = R.color.magnitude1;
                break;
            case 2:
                magnitudeColorResourceId = R.color.magnitude2;
                break;
            case 3:
                magnitudeColorResourceId = R.color.magnitude3;
                break;
            case 4:
                magnitudeColorResourceId = R.color.magnitude4;
                break;
            case 5:
                magnitudeColorResourceId = R.color.magnitude5;
                break;
            case 6:
                magnitudeColorResourceId = R.color.magnitude6;
                break;
            case 7:
                magnitudeColorResourceId = R.color.magnitude7;
                break;
            case 8:
                magnitudeColorResourceId = R.color.magnitude8;
                break;
            case 9:
                magnitudeColorResourceId = R.color.magnitude9;
                break;
            default:
                magnitudeColorResourceId = R.color.magnitude10plus;
                break;
        }

        return ContextCompat.getColor(getContext(),magnitudeColorResourceId);
    }





}

in last line "ContextCompat.getColor(getContext(),magnitudeColorResourceId);"...i am getting error of get context .

Amit Kumar
  • 33
  • 7

1 Answers1

0

you cannot use getContext() in adapter class instead you can pass context from your activity where you set your adapter to recyclerview througth adapter's parameter or you can do like this instead : declare context in myAdapter class

Context context;

 @NonNull
    @Override
    public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       // you can get context from this line
        context = parent.getContext();

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row,parent,false);
        return new myViewHolder(view);
    }
D_K
  • 152
  • 2
  • 9