如何在片段中初始化接口构造函数?

时间:2019-06-17 13:21:40

标签: java android android-fragments interface

在初始化cgvReferentiel.DataBind(); 类内的接口类时发生错误,构造函数中的参数对我来说并不明确。我该如何解决这个问题?

我遇到此错误:

Fragment

这是我的片段类

     FATAL EXCEPTION: main
        Process: com.example.finalui, PID: 7965
        java.lang.ClassCastException: com.example.finalui.ApplicationVariable cannot be cast to com.example.finalui.VvVolleyInterface
            at com.example.finalui.VvVolleyClass.<init>(VvVolleyClass.java:35)
            at com.example.finalui.fragments.itemcommfragment.getCommentsfromJson(itemcommfragment.java:100)
            at com.example.finalui.fragments.itemcommfragment.sendtheComment(itemcommfragment.java:95)
            at com.example.finalui.fragments.itemcommfragment.check(itemcommfragment.java:89)
            at com.example.finalui.fragments.itemcommfragment.access$000(itemcommfragment.java:39)
            at com.example.finalui.fragments.itemcommfragment$1.onClick(itemcommfragment.java:72)....

这是我的界面类

    package com.example.finalui.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.finalui.Adapters.MyAdapter4;
import com.example.finalui.ApplicationVariable;
import com.example.finalui.Models.CommentModel;
import com.example.finalui.R;
import com.example.finalui.Adapters.MyAdapter4;
import com.example.finalui.Models.CommentModel;
import com.example.finalui.R;
import com.example.finalui.TasksModel;
import com.example.finalui.VvVolleyClass;
import com.example.finalui.VvVolleyInterface;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class itemcommfragment extends Fragment implements VvVolleyInterface {

    View view;
    RecyclerView recyclerView2;
    MyAdapter4 myAdapter4;
    EditText comment;
    Button button;
    List<CommentModel> commentModelList;
    String slip_no;

    public itemcommfragment(){}

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.item_comment,container,false);
        Intent intent = getActivity().getIntent();
        Bundle args = intent.getBundleExtra("BUNDLE2");
        commentModelList=new ArrayList<>();


        Intent intent2 = getActivity().getIntent();
        TasksModel tasksModel = (TasksModel) intent2.getSerializableExtra("INFO");
        slip_no=  tasksModel.getSlip_no();

        ArrayList<CommentModel> object = (ArrayList<CommentModel>) args.getSerializable("ARRAYLIST2");
        commentModelList=object;
        recyclerView2=view.findViewById(R.id.recyclerview2);
        comment=view.findViewById(R.id.editText3);
        button=view.findViewById(R.id.button4);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check();
            }
        });

        myAdapter4=new MyAdapter4(getContext(), commentModelList);
        recyclerView2.setAdapter(myAdapter4);
        return view;
    }

    private void check() {
        if(TextUtils.isEmpty(comment.getText().toString().trim()))
        {
           comment.setError("Please give some comment");
        }
        else
        {
            String commentxt = comment.getText().toString().trim();
            sendtheComment(commentxt);

        }
    }

    private void sendtheComment(String commentxt) {
        getCommentsfromJson(commentxt);
    }

    private void getCommentsfromJson(String commentxt) {

        VvVolleyClass vvVolleyClass = new VvVolleyClass(getActivity().getApplicationContext(),getActivity().getApplicationContext());
        HashMap params = new HashMap<>();
        params.put("phone", ApplicationVariable.ACCOUNT_DATA.contact);
        params.put("token", ApplicationVariable.ACCOUNT_DATA.token);
        params.put("regId", ApplicationVariable.ACCOUNT_DATA.reg_id);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("slip_no",slip_no);
        jsonObject.addProperty("comment",commentxt);
        params.put("new_data_row", jsonObject);
        vvVolleyClass.makeRequest("http://admin.doorhopper.in/api/vdhp/order/slip/comment/create", params);
    }

    @Override
    public void onTaskComplete(String result) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            if(jsonObject.getString("responseFor").equals( "order/slip/comment/create")){
                comment.setText("");
                getCommentRefreshed();
            }

            if(jsonObject.getString("responseFor").equals( "order/slip/comment/get")){
               if(jsonObject.getInt("data_rows_size")>0){
                   JSONArray jarray = jsonObject.getJSONArray("data_rows");
                   for(int i=0;i<jarray.length();i++){
                       CommentModel commentModel = new CommentModel(jarray.getJSONObject(i).getString("staff"),
                               jarray.getJSONObject(i).getString("comment"),
                               jarray.getJSONObject(i).getString("commented_on"),
                               jarray.getJSONObject(i).getString("slip_no"),
                               jarray.getJSONObject(i).getString("id"));
                       commentModelList.add(commentModel);
                   }
                   myAdapter4.notifyDataSetChanged();
               }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getCommentRefreshed() {
        commentModelList=new ArrayList<>();

        VvVolleyClass vvVolleyClass = new VvVolleyClass(getActivity().getApplicationContext(),getActivity().getApplicationContext());
        HashMap params = new HashMap<>();
        params.put("phone", ApplicationVariable.ACCOUNT_DATA.contact);
        params.put("token", ApplicationVariable.ACCOUNT_DATA.token);
        params.put("regId", ApplicationVariable.ACCOUNT_DATA.reg_id);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("slip_no",slip_no);
        params.put("filter", jsonObject);
        vvVolleyClass.makeRequest("http://admin.doorhopper.in/api/vdhp/order/slip/comment/get", params);
    }
}

欢迎任何建议,推荐和评论

0 个答案:

没有答案