当前位置: 首页 > 知识库问答 >
问题:

Firebase连接Cardview和RecycerView的Fragment中的NoSuchMethodError

董高畅
2023-03-14
mDatabase = FirebaseDatabase.getInstance().getReference().child("Incidents");
package com.example.samuel.a4;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import org.w3c.dom.Text;


public class IncmanFragment extends Fragment {

private RecyclerView mBlogList;
private DatabaseReference mDatabase;

FloatingActionButton fab;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_incman, container, 
false);
     //i moved this up from just ontop of fab to here
    View myView = inflater.inflate(R.layout.fragment_incman, container, 
false);
   // getFragmentManager().findFragmentById(R.id.fragment_container);
    mDatabase = 
FirebaseDatabase.getInstance().getReference().child("Incidents");
    mDatabase.keepSynced(true);

    mBlogList=(RecyclerView) myView.findViewById(R.id.recyclerview);
    mBlogList.setHasFixedSize(true);
    mBlogList.setLayoutManager(new LinearLayoutManager(getActivity()));


    fab = (FloatingActionButton) myView.findViewById(R.id.fabIncident);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentTransaction fragmentTransaction = 
getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, new 
ReportFragment());
            fragmentTransaction.addToBackStack(null);
          /*  Toast.makeText(getActivity(), "toast successful", 
Toast.LENGTH_SHORT).show();*/
            fragmentTransaction.commit();

        }

    });

    return myView;
}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<IncmanFirebaseName, IncmanViewHolder> 
    firebaseRecyclerAdapter = new 
    FirebaseRecyclerAdapter<IncmanFirebaseName, 
    IncmanViewHolder>
            (IncmanFirebaseName.class, R.layout.incidentdesign, 
      IncmanViewHolder.class, mDatabase) {
        @Override
        protected void populateViewHolder(IncmanViewHolder viewHolder, 

            viewHolder.setTitle(model.getTitle());
            viewHolder.setDetails(model.getDetailsId());
            viewHolder.setRemarks(model.getRemarksId());
            viewHolder.setLocation(model.getLocationId());
            viewHolder.setDate(model.getDate());
            viewHolder.setUrgency(model.getUrgencyId());

        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);

}

    public static class IncmanViewHolder extends RecyclerView.ViewHolder
    {
        View mView;
        public IncmanViewHolder(View itemView)
        {
            super(itemView);
            mView = itemView;
        }
        public void setTitle(String title)
        {
            TextView tvtitledesign = 
    (TextView)mView.findViewById(R.id.tvtitledesign);
            tvtitledesign.setText(title);
        }
        public void setDetails(String detailsId)
        {
            TextView tvdetailsdesign = 
    (TextView)mView.findViewById(R.id.tvdetailsdesign);
            tvdetailsdesign.setText(detailsId);
        }
        public void setRemarks(String remarksId)
        {
            TextView tvremarksdesign = 
    (TextView)mView.findViewById(R.id.tvremarksdesign);
            tvremarksdesign.setText(remarksId);
        }
        public void setLocation(String locationId)
        {
            TextView tvlocationdesign = (TextView) 
    mView.findViewById(R.id.tvlocationdesign);
            tvlocationdesign.setText(locationId);

        }
        public void setDate (String date)
        {
            TextView tvdatedesign = 
     (TextView)mView.findViewById(R.id.tvdatedesign);
            tvdatedesign.setText(date);
        }
        public void setUrgency(String urgencyId)
        {
            TextView tvurgencydesign = 
     (TextView)mView.findViewById(R.id.tvurgencydesign);
            tvurgencydesign.setText(urgencyId);
        }
    }
}
 package com.example.samuel.a4;

    public class IncmanFirebaseName {

    private String date;
    private String detailsId;
    private String locationId;
    private String remarksId;
    private String reportedById;
    private String splocationId;
    private String typeId;
    private String urgencyId;
    private String title;

    public IncmanFirebaseName(String date, String detailsId, String 
    remarksId/*String reportedById*/ /* String typeId*/, String urgencyId, 
    String 
    title) {
        this.date = date;
        this.detailsId = detailsId;
      /*  this.locationId = locationId;*/
        this.remarksId = remarksId;
        /*this.reportedById = reportedById;*/
        this.splocationId = splocationId;
        /*this.typeId = typeId;*/
        this.urgencyId = urgencyId;
        this.title=title;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDetailsId() {
        return detailsId;
    }

    public void setDetailsId(String detailsId) {
        this.detailsId = detailsId;
    }

    public String getLocationId() {
        return locationId;
    }

    public void setLocationId(String locationId) {
        this.locationId = locationId;
    }

    public String getRemarksId() {
        return remarksId;
    }

    public void setRemarksId(String remarksId) {
        this.remarksId = remarksId;
    }

    /*public String getReportedById() {
        return reportedById;
    }

    public void setReportedById(String reportedById) {
        this.reportedById = reportedById;
    }*/

    /* public String getSplocationId() {
        return splocationId;
    }

    public void setSplocationId(String splocationId) {
        this.splocationId = splocationId;
    }*/

     /*   public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }*/

    public String getUrgencyId() {
        return urgencyId;
    }

    public void setUrgencyId(String urgencyId) {
        this.urgencyId = urgencyId;
    }

    public String getTitle(){
        return title;
    }
    public void setTitle() {
        this.title = title;
    }
    public IncmanFirebaseName()
    {

    }
    }
    import android.app.DatePickerDialog;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.media.Image;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.text.TextUtils;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.OnFailureListener;
    import com.google.android.gms.tasks.OnSuccessListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.storage.FirebaseStorage;
    import com.google.firebase.storage.OnProgressListener;
    import com.google.firebase.storage.StorageReference;
    import com.google.firebase.storage.UploadTask;

    import org.w3c.dom.Text;

    import java.io.IOException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.UUID;

    import static android.app.Activity.RESULT_OK;

    public class ReportFragment extends Fragment implements 
    AdapterView.OnItemSelectedListener{

    private Button btnChoose,btnUpload;
    private ImageView imageView;
    private Uri filePath;
    private final int PICK_IMAGE_REQUEST = 71;
    private TextView mDisplayDate;
    private DatePickerDialog.OnDateSetListener mDateSetListener;

    private EditText reportedBy;
    private TextView date; //upstairs declare alr as mDisplayDate
    private Spinner spinner; //downstairs declare alr as spinner3
    private EditText location;
    private Spinner spinner2; //downstairs declare alr as spinner2
    private EditText details;
    private Spinner spinner3; //downstairs declare alr as spinner1
    private EditText remarks;
    private EditText title;
    private Button submitIncident;

    DatabaseReference databaseIncidents;
    FirebaseStorage storage;
    StorageReference storageReference;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
    ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_report, container, 
false);

          databaseIncidents = 
    FirebaseDatabase.getInstance().getReference("Incidents");
        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();

        btnChoose = (Button) v.findViewById(R.id.btnChoose);
        btnUpload = (Button) v.findViewById(R.id.btnUpload);
        imageView = (ImageView) v.findViewById(R.id.imgView);

        reportedBy = (EditText) v.findViewById(R.id.etreportedby);
    //        Below have already line 151
    //        date = (TextView) v.findViewById(R.id.tvdate);
        location = (EditText) v.findViewById(R.id.etlocation);
        details = (EditText) v.findViewById(R.id.etdetails);
        remarks = (EditText) v.findViewById(R.id.etremarks);
        title = (EditText) v.findViewById(R.id.ettitle);
        submitIncident = (Button) v.findViewById(R.id.btnSubmit);


        btnChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseImage();
            }
        });
        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        });

        submitIncident.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                incidentSubmit();
            }
        });

        return v;


    }

    private void incidentSubmit(){
        String reportedname = reportedBy.getText().toString().trim();
        String location1 = location.getText().toString();
        String details1 = details.getText().toString();
        String remarks1 = remarks.getText().toString();
        String urgency1 = spinner.getSelectedItem().toString();
        String type1 = spinner2.getSelectedItem().toString();
        String splocation1 = spinner3.getSelectedItem().toString();
        String date1 = mDisplayDate.getText().toString();
        String title1 = title.getText().toString();

        if(!TextUtils.isEmpty(reportedname)) {

            String incidentId = databaseIncidents.push().getKey();
            Incident incident = new 

  Incident(reportedname,location1,details1,remarks1,urgency1,type1,splocation1,date1,title1);
            databaseIncidents.child(incidentId).setValue(incident);

            Toast.makeText(getActivity(), "Incident Added", 
    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "All fields must be 
    entered",Toast.LENGTH_LONG).show();
        }

    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, 
    long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    }

任何帮助都是真诚的感谢。谢谢!

共有1个答案

柳羽
2023-03-14
mDatabase = FirebaseDatabase.getInstance().getReference().child("Incidents");
mDatabase.child("LR9j...").addListenerForSingleValueEvent(new ValueEventListener() {
// in the child("LR9j..") you have to put the whole first id from your image
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String locId = dataSnapshot.child("locationId").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "onCancelled", databaseError.toException());
            }


        });
 类似资料:
  • 我使用的是recylcer视图,每一行都包含cardview,即使我的textview内容较少,我也需要显示cardview的全宽度。我尝试了很多方法,但都没用。 XML 即使我设置宽度来匹配父级,但没有用。 充气文档: 从指定的xml资源膨胀一个新的视图层次结构。如果出现错误,则引发IflateException。 要加载的XML布局资源(例如,r.layout.main_page)根视图的资源

  • 问题解决了:我尝试了fmt.println.mko评论中的这个解决方案 您可以这样做,定义2个项目类型,一个用于您通常的卡片,一个用于您的2.RecycerView,使用int getItemViewType(int位置)返回特定类型。在onCreateViewHolder(ViewGroup parent,int viewType)中,您根据类型创建一个viewholder,可以是您的卡,也可以

  • 并且Ive在我的AndroidManifest.xml中定义了新的活动,但是我不知道如何解决这个问题······ 我尝试了很多答案在Stackoverflow和其他网站上找到,但我不能解决这个问题...

  • 主要内容:检查连接在本章中,我们将演示如何处理Firebase连接状态。 检查连接 我们可以使用下面的代码检查连接值。 当运行应用程序时,弹出窗口会通知有关连接。 通过使用上面给出的功能,可以跟踪连接状态并相应地更新您的应用程序。

  • 我对Firebase定价模型有点困惑,特别关注的是连接或更准确地说是并发连接。 让我们举一个iOS移动应用程序FanZONE的例子: 用户将在足球比赛期间创建组。他们将对实际比赛进行评论,评论将立即显示在每个会员屏幕上。 这种情况是否意味着参与评论/查看的每个用户都会对一个连接计数 那么,如果一个组包含100个积极关注屏幕并不时发表评论的用户,那么每90分钟是否有100个连接 后台有应用程序的用户

  • 我正在尝试将我的Java web项目连接到Firebase,这样我就可以将数据存储在FireStore中。我正在使用Java管理SDK,遵循官方指南:FireBaseAdminSDK。不幸的是,在初始化FirebaseAp时,我遇到了异常: 现在,我读到这里和那里,它的问题与guava项目,建议使用不同版本的guava库。我试过从18开始的所有版本。 我甚至下载了google firebase s