无法解析“myAdapter”中的方法“getContext”[重复]

Posted

技术标签:

【中文标题】无法解析“myAdapter”中的方法“getContext”[重复]【英文标题】:Cannot resolve method 'getContext' in 'myAdapter' [duplicate] 【发布时间】:2021-11-04 20:31:24 【问题描述】:

我收到上下文错误。它显示 getcontext 无法在我的适配器类中解析。 我正在创建一个地震报告应用程序,就像 udacity 课程中的应用程序一样。我添加了回收站视图和卡片视图。因此我创建了一个适配器类。 当我开始为我的幅度文本视图添加颜色时,我创建了一种获取幅度颜色方法的方法,在该方法中我必须从 colors.xml 文件中取出颜色,但它需要上下文并且它说上下文无法解析。 我正在提供我的应用程序的代码...请帮助我,我是 android 开发的新手

我的适配器类代码:-

包 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 .

【问题讨论】:

【参考方案1】:

您不能在适配器类中使用 getContext() 而是可以从您的活动中传递上下文,您可以通过适配器的参数将适配器设置为 recyclerview,或者您可以这样做: 在 myAdapter 类中声明上下文

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);
    

【讨论】:

非常感谢

以上是关于无法解析“myAdapter”中的方法“getContext”[重复]的主要内容,如果未能解决你的问题,请参考以下文章

IBM Worklight 中的 Json 解析

当RecyclerView位于对话框上时,不显示分隔符

安卓项目中,继承BaseAdapter重写适配器MyAdapter,适配器中监听按钮,如何实现点击按钮弹出一个对话框?

将 myAdapter 合成转换为视图绑定

MyAdapter.java

magento完全解析cms块