gpt4 book ai didi

android - 无法在适配器中使用 Intent

转载 作者:行者123 更新时间:2023-11-29 15:36:16 25 4
gpt4 key购买 nike

在触发 Intent finish Activity 时出现此错误

03-20 11:05:16.991 8566-8566/com.example.jenya1.ebilling E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jenya1.ebilling, PID: 8566
java.lang.ClassCastException: droidninja.filepicker.FilePickerDelegate cannot be cast to android.app.Activity
at com.example.jenya1.ebilling.adapter.MaterialAdapter$1$1.onClick(MaterialAdapter.java:142)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

这一行导致错误

((Activity)mContext).finish();    //error

调用应用类

private void loadMaterialRequest() {

final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
//getting the progressbar

//making the progressbar visible
progressBar.setVisibility(View.VISIBLE);

albumList.clear();

//creating a string request to send request to the url
StringRequest stringRequest = new StringRequest(Request.Method.GET, HttpUrl+token+"&type="+type,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//hiding the progressbar after completion
progressBar.setVisibility(View.INVISIBLE);

try {
//getting the whole json object from the response
JSONObject obj = new JSONObject(response);

JSONArray heroArray = obj.getJSONArray("response");

//now looping through all the elements of the json array
for (int i = 0; i < heroArray.length(); i++) {
//getting the json object of the particular index inside the array
JSONObject heroObject = heroArray.getJSONObject(i);

//creating a hero object and giving them the values from json object
Material hero = new Material(
heroObject.getInt("Request_id"),
heroObject.getString("Site_name"),
heroObject.getString("User_id"),
heroObject.getString("Item_name"),
heroObject.getString("Quantity"),
heroObject.getString("Country"),
heroObject.getString("Request_date"),
heroObject.getString("Request_status"));

//adding the hero to herolist
albumList.add(hero);
}
//Log.e("list", String.valueOf(albumList));
//creating custom adapter object
adapter = new MaterialAdapter(getApplicationContext(),albumList);

//adding the adapter to listview
recyclerView.setAdapter(adapter);

} catch (JSONException e) {

e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

Log.e("error", String.valueOf(error));

}
});

//creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);

//adding the string request to request queue
requestQueue.add(stringRequest);
}

当用户单击警报对话框是按钮时,我正在尝试 Intent

这是我的适配器类:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jenya1.ebilling.MaterialHistoryActivity;
import com.example.jenya1.ebilling.R;
import com.example.jenya1.ebilling.model.HistoryQuotation;
import com.example.jenya1.ebilling.model.Material;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Mitesh Makwana on 18/03/18.
*/
public class MaterialAdapter extends RecyclerView.Adapter<MaterialAdapter.MyViewHolder> {

private Context mContext;
private List<Material> albumList;
private List lstfavquoteid;


String quote_id;
ArrayList<Integer> arryfavquoteid;

Typeface tf;
ArrayList<String> imageserver,imageList;
int count;

int status=0;
private int mExpandedPosition=-1;

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title,quantity,site,date,note,req_id,status;

ImageView delete;
LinearLayout lndetail,lnapprove,lnreject,lnoperation;
CardView cdview;

public MyViewHolder(View view) {
super(view);

title = (TextView) view.findViewById(R.id.txttitle);
req_id = (TextView) view.findViewById(R.id.txtreqid);
quantity = (TextView) view.findViewById(R.id.txtquantity);
date = (TextView) view.findViewById(R.id.txtdate);
site = (TextView) view.findViewById(R.id.txtsite);
note = (TextView) view.findViewById(R.id.txtnotes);
status = (TextView) view.findViewById(R.id.txtstatus);

delete=(ImageView)view.findViewById(R.id.imgmdelete);

lndetail = (LinearLayout) view.findViewById(R.id.lndetails);
lnapprove = (LinearLayout) view.findViewById(R.id.lnapprove);
lnreject = (LinearLayout) view.findViewById(R.id.lnreject);
lnoperation = (LinearLayout) view.findViewById(R.id.lnstatusoperation);
cdview = (CardView) view.findViewById(R.id.card_view);
}
}

public MaterialAdapter(Context mContext, List<Material> albumList) {
this.mContext = mContext;
this.albumList = albumList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.raw_material_history, parent, false);

return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {

arryfavquoteid = new ArrayList<Integer>();
imageserver = new ArrayList<String>();

tf = Typeface.createFromAsset(mContext.getAssets(), "fonts/HKNova-Medium.ttf");
holder.title.setTypeface(tf);
holder.req_id.setTypeface(tf);
holder.quantity.setTypeface(tf);
holder.date.setTypeface(tf);
holder.site.setTypeface(tf);
holder.note.setTypeface(tf);

final Material album = albumList.get(position);

holder.title.setText(album.getItem_name());
holder.req_id.setText("#"+album.getId());
holder.quantity.setText(album.getQuantity());
holder.site.setText(album.getSite_id());

String input = album.getRequest_date();
input = input.replace(" ", "\n");
holder.date.setText(input);
holder.note.setText("Notes : "+album.getCountry());

holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(mContext, "Delete...", Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(
view.getContext());
alert.setTitle("Alert!!");
alert.setMessage("Are you sure to delete record");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
MaterialHistoryActivity.deleterequest(mContext,album.getId());

notifyDataSetChanged();

Intent i=new Intent(mContext,MaterialHistoryActivity.class);
mContext.startActivity(i);
((Activity)mContext).finish(); //error

dialog.dismiss();

}
});
alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

alert.show();
}
});
if (mExpandedPosition == position) {

if(album.getRequest_status().equals("1"))
{
holder.status.setText("Approved");
holder.status.setVisibility(View.VISIBLE);
holder.status.setTextColor(Color.GREEN);
holder.lnoperation.setVisibility(View.GONE);
}else if(album.getRequest_status().equals("2"))
{
holder.status.setText("Rejected");
holder.status.setVisibility(View.VISIBLE);
holder.status.setTextColor(Color.RED);
holder.lnoperation.setVisibility(View.GONE);
}
else
{
holder.status.setVisibility(View.GONE);
holder.lnoperation.setVisibility(View.VISIBLE);
}

//creating an animation
Animation slideDown = AnimationUtils.loadAnimation(mContext, R.anim.slide_down);

//toggling visibility
holder.lndetail.setVisibility(View.VISIBLE);

//adding sliding effect
holder.lndetail.startAnimation(slideDown);
}
else
{
holder.lndetail.setVisibility(View.GONE);
}

holder.cdview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//getting the position of the item to expand it
mExpandedPosition = position;

//reloding the list
notifyDataSetChanged();
}
});


holder.lnapprove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(mContext, "Approve...", Toast.LENGTH_SHORT).show();
MaterialHistoryActivity.materialpermission(mContext,album.getId(),1);
}
});

holder.lnreject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(mContext, "Reject...", Toast.LENGTH_SHORT).show();
MaterialHistoryActivity.materialpermission(mContext,album.getId(),2);

}
});

}

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

如有任何帮助,我们将不胜感激。

最佳答案

让我们看看:

((Activity)mContext).finish(); 

日志说 mContext 是一种 FilePickerDelegate。因此,请检查您的 Adapter init 函数。我认为您将上下文作为“this”传递,但“this”是对 FilePickerDelegate 的引用。所以将其更改为 YourActivity.this(带有 Activity )或 getActivity(在 fragment 中)

关于android - 无法在适配器中使用 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49377267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com