我无法解决这个错误。有人能帮我吗?

时间:2017-06-27 13:28:29

标签: android firebase textview

错误在最后一个单词"(名称)"和UsersViewHolder.setName(users.getName());,仅.setName

它说制造"静电" " setName(String name)"在第三个到最后一个条带,但如果我这样做,倒数第二个条带中的mView会出错。

package com.app.andre.easychat;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.w3c.dom.Text;
public class UsersActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView users_list;
private DatabaseReference mdatabase;

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

    mToolbar= (Toolbar) findViewById(R.id.user_appbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Tutti gli utenti");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mdatabase= FirebaseDatabase.getInstance().getReference().child("Users");

    users_list = (RecyclerView) findViewById(R.id.users_list);
    users_list.setHasFixedSize(true);
    users_list.setLayoutManager(new LinearLayoutManager(this) );
}

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<Users, UsersViewHolder> FirebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(
            Users.class,
            R.layout.name_list,
            UsersViewHolder.class,
            mdatabase
    ) {
        @Override
        protected void populateViewHolder(UsersViewHolder viewHolder, Users users, int position) {
            UsersViewHolder.setName(users.getName());
        }
    };

    users_list.setAdapter(FirebaseRecyclerAdapter);
}


public static class UsersViewHolder extends RecyclerView.ViewHolder {

    View mView;
    public UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;}

        public  void setName(String name) {

            TextView mUsernameView = (TextView) mView.findViewById(R.id.user_single_name);
            mUsernameView.getText(name);

        }
    }
}

1 个答案:

答案 0 :(得分:3)

我认为你需要

viewHolder.setName(users.getName());

而不是

UsersViewHolder.setName(users.getName());

您试图将ViewHolder用作静态引用,而不是使用recyclerview适配器创建的类的实例。