从扩展类调用异步 - 错误不是封闭类
Posted
技术标签:
【中文标题】从扩展类调用异步 - 错误不是封闭类【英文标题】:Call Async From An Extending Class - Error is not an enclosing class 【发布时间】:2021-03-10 16:01:31 【问题描述】:我有一个扩展 RecyclerViewer 类 (MyRecyclerViewAdapater) 的类 (DisplayIMageFromFTP_3)
public class DisplayImageFromFTP_3 extends AppCompatActivity implements MyRecyclerViewAdapter_3.ItemClickListener
我如何填充适配器
private void callRecyclerAdapter()
adapter = new MyRecyclerViewAdapter_3(this,
arrayListString_url_abs_fileName,
arrayListString_url_fileName,
arrayListString_url_fileExtension,
arrayListString_url_fileDate,
arrayListString_url_fileSize);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
MyRecyclerViewAdapter_3:
class MyRecyclerViewAdapter_3 extends RecyclerView.Adapter<MyRecyclerViewAdapter_3.ViewHolder>
private ArrayList<String> arrayListString_url_abs_fileName,
arrayListString_url_fileName,
arrayListString_url_fileExtension,
arrayListString_url_fileDate,
arrayListString_url_fileSize;
String url_abs_fileName,
url_fileName,
url_fileExtension,
url_fileDate,
url_fileSize;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
PopupWindow popupWindow;
private Context pContext, aContext; //Context for Popup Window (got from onCreateViewHolder)
// data is passed into the constructor
MyRecyclerViewAdapter_3(Context context,
ArrayList<String> url_abs_fileName,
ArrayList<String> url_fileName,
ArrayList<String> url_fileExtension,
ArrayList<String> url_fileDate,
ArrayList<String> url_fileSize)
this.mInflater = LayoutInflater.from(context);
this.arrayListString_url_abs_fileName = url_abs_fileName;
this.arrayListString_url_fileName = url_fileName;
this.arrayListString_url_fileExtension = url_fileExtension;
this.arrayListString_url_fileDate = url_fileDate;
this.arrayListString_url_fileSize = url_fileSize;
// inflates the row layout from xml when needed
@Override
@NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
pContext = parent.getContext();
return new ViewHolder(view);
// binds the data to the view
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
url_abs_fileName = arrayListString_url_abs_fileName.get(position);
url_fileName = arrayListString_url_fileName.get(position);
url_fileExtension = arrayListString_url_fileExtension.get(position);
url_fileDate = arrayListString_url_fileDate.get(position);
url_fileSize = arrayListString_url_fileSize.get(position);
Context mContext = holder.ivHorizontalGrid.getContext();
Glide.with(mContext)
.load(url_abs_fileName)
.thumbnail(0.05f)
//.diskCacheStrategy(DiskCacheStrategy.DATA)
.placeholder(R.drawable.loading)
.error(R.drawable.error).
into(holder.ivHorizontalGrid)
;
holder.ibDots.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view_item)
url_abs_fileName = arrayListString_url_abs_fileName.get(position);
url_fileName = arrayListString_url_fileName.get(position);
url_fileExtension = arrayListString_url_fileExtension.get(position);
url_fileDate = arrayListString_url_fileDate.get(position);
url_fileSize = arrayListString_url_fileSize.get(position);
setupPopUpWindow(view_item);
);
// total number of rows
@Override
public int getItemCount()
return arrayListString_url_abs_fileName.size();
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
ImageView ivHorizontalGrid;
ImageButton ibDots;
ViewHolder(View itemView)
super(itemView);
ivHorizontalGrid = itemView.findViewById(R.id.ivHorizontalGrid);
ibDots = itemView.findViewById(R.id.ibDots);
itemView.setOnClickListener(this);
@Override
public void onClick(View view)
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
//Log.d("LOG", "zzz_Click: " +view);
// convenience method for getting data at click position
public String getItem(int id)
return arrayListString_url_abs_fileName.get(id);
// allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener)
this.mClickListener = itemClickListener;
// parent activity will implement this method to respond to click events
public interface ItemClickListener
void onItemClick(View view, int position);
//Popup Window
private void setupPopUpWindow(View view_item)
LayoutInflater inflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view_layout;
view_layout = inflater.inflate(R.layout.dots_menu_layout, null);
popupWindow = new PopupWindow(view_layout,560, 740, true);
popupWindow.showAsDropDown(view_item,-153,0);
//Display FileName
TextView tvFileName = view_layout.findViewById(R.id.tvFileName);
tvFileName.setText(url_fileName);
tvFileName.setSelected(true); //android:ellipsize="marquee"
//Display Sub Text (Extension . Date . Size)
TextView tvSub = view_layout.findViewById(R.id.tvSub);
tvSub.setText(url_fileExtension.toUpperCase() +" \u2022 "+url_fileDate +" \u2022 " +url_fileSize);
//MENU ~ SHARE
LinearLayout ll_share;
ll_share = view_layout.findViewById(R.id.ll_share);
ll_share.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
downloadFileForShare();
private void downloadFileForShare()
new DisplayImageFromFTP_3.Async_GetImageFromFTP(this).execute();
);
现在我正在尝试从 DisplayImageFromFTP_3 类调用异步任务,如下所示:
new DisplayImageFromFTP_3.Async_GetImageFromFTP(this).execute();
并收到此错误:
Error: DisplayImageFromFTP_3 is not an enclosing class
我想避免使用 Android Studio 建议的静态,因为它会导致 DisplayImageFromFTP_3 中的许多对象出现问题,所以也许我需要正确的上下文?
【问题讨论】:
【参考方案1】:感谢这个答案:
https://***.com/questions/7619505/how-can-i-resolve-an-enclosing-instance-that-contains-x-y-is-required
我能够使用这个逻辑:
DisplayImageFromFTP_3 x = new DisplayImageFromFTP_3();
DisplayImageFromFTP_3.Async_GetImageFromFTP y = x.new Async_GetImageFromFTP(pContext);
y.execute();
【讨论】:
以上是关于从扩展类调用异步 - 错误不是封闭类的主要内容,如果未能解决你的问题,请参考以下文章