在onCreateViewHolder

时间:2018-05-27 16:32:24

标签: android firebase firebase-realtime-database

我是android studio的新手,onCreateViewHolder里面的getRef(位置)

getref未被识别为onCreateViewHolder

中的命令

显示错误是否有任何方法可以解决问题,尝试将用户信息检索到另一个页面但是遇到了这个问题

暂时播放此错误请尽快解答

enter image description here

public class Main2Activity extends AppCompatActivity {

    RecyclerView myRecyclerView;
    MyAdapter adapter;
    List<MyDataGetSet> listData;
    FirebaseDatabase FDB;
    DatabaseReference DBR;
    android.support.v7.widget.Toolbar mToolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);


        mToolbar=(android.support.v7.widget.Toolbar)findViewById(R.id.register_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("All Users");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        myRecyclerView=(RecyclerView)findViewById(R.id.MyReclycler);
        myRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager LM= new LinearLayoutManager(getApplicationContext());
        myRecyclerView.setLayoutManager(LM);
        myRecyclerView.setItemAnimator(new DefaultItemAnimator());
        myRecyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayout.VERTICAL));

        listData=new ArrayList<>();

        adapter=new MyAdapter(getApplicationContext(),listData);
        FDB=FirebaseDatabase.getInstance();
        GetDataFirebase();

    }
    void  GetDataFirebase(){
        DBR=FDB.getReference("Users");

        DBR.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                MyDataGetSet data=dataSnapshot.getValue(MyDataGetSet.class);
                listData.add(data);
                myRecyclerView.setAdapter(adapter);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s)
            {
                for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
                    String visit_user_id = childSnapshot.getKey();
                }
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }


    public  class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
        private Context context;
        List<MyDataGetSet> listArray;

        public   MyAdapter(Context context,List<MyDataGetSet> List){
            this.context=context;
            this.listArray=List;
        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.itemview,parent,false);
            return new MyViewHolder(view);
        }



        @Override
        public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, final int position) {
            MyDataGetSet data= listArray.get(position);
            holder.MyText.setText(data.getUser_name());
            holder.MyStatus.setText(data.getUser_status());


            Picasso.with(context).load(data.getUser_thumb_image()).into(holder.MyImage);



            holder.itemView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    String visit_user_id =getRef().getKey();
                    Intent profileIntend = new Intent(Main2Activity.this,ProfileActivity.class);
                     profileIntend.putExtra("visit_user_id",visit_user_id);
                    startActivity(profileIntend);
                }
            });
        }

        public  class MyViewHolder extends RecyclerView.ViewHolder{

            TextView MyText;
            TextView MyStatus;
           CircleImageView MyImage;

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


                MyText=(TextView)itemView.findViewById(R.id.TextView);
                MyStatus=(TextView)itemView.findViewById(R.id.TextStatus);
                MyImage=(CircleImageView) itemView.findViewById(R.id.TextImage);

            }
        }

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



}

1 个答案:

答案 0 :(得分:0)

要解决此问题,请更改以下代码行:

String visit_user_id =getRef().getKey();

MyDataGetSet myDataGetSet = getItem(position);

现在有一个MyDataGetSet类的对象,你可以得到这样的密钥:

String visit_user_id = myDataGetSet.getVisitUserKey();

假设您已将该密钥存储在myDataGetSet对象中。