无法解析 'myAdapter' 中的方法 'getContext'
Cannot resolve method 'getContext' in 'myAdapter'
我收到上下文错误。它显示无法在我的适配器 class 中解析 getcontext。
我正在创建一个地震报告应用程序,就像 udacity 课程中的应用程序一样。我添加了回收站视图和卡片 view.therefore 我做了一个适配器 class。
当我开始向我的幅度文本视图添加颜色时,我创建了一种获取幅度颜色方法的方法,在该方法中我必须从 colors.xml 文件中取出颜色,但它需要上下文并且它说上下文无法解析。
我正在提供我的应用程序代码...请帮助我,我是 android 开发的新手
我的适配器class代码:-
包 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 .
你不能在适配器 class 中使用 getContext() 而不是你可以从你的 activity 传递上下文,在那里你通过适配器的参数将你的适配器设置为 recyclerview 或者你可以这样做:
在 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);
}
我收到上下文错误。它显示无法在我的适配器 class 中解析 getcontext。 我正在创建一个地震报告应用程序,就像 udacity 课程中的应用程序一样。我添加了回收站视图和卡片 view.therefore 我做了一个适配器 class。 当我开始向我的幅度文本视图添加颜色时,我创建了一种获取幅度颜色方法的方法,在该方法中我必须从 colors.xml 文件中取出颜色,但它需要上下文并且它说上下文无法解析。 我正在提供我的应用程序代码...请帮助我,我是 android 开发的新手
我的适配器class代码:-
包 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 .
你不能在适配器 class 中使用 getContext() 而不是你可以从你的 activity 传递上下文,在那里你通过适配器的参数将你的适配器设置为 recyclerview 或者你可以这样做: 在 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);
}