Firebase Recycler Adapter:应用程序在调用getItemViewType()方法时崩溃

时间:2018-05-26 05:42:21

标签: android firebase listview android-recyclerview recycler-adapter

我正在使用Firebase Recycler Adapter,当我向我的适配器添加 getItemViewType() getItemId()方法时,我的应用程序崩溃。

如果从我的适配器中删除这两个方法然后在回滚期间回收物品随机播放项目,它可以正常工作。

请帮我解决这个问题,

提前致谢!

例外/错误:

android.content.res.Resources$NotFoundException: Resource ID #0x0
    at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:216)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2140)
    at android.content.res.Resources.getLayout(Resources.java:1147)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
    at com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:195)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
    at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1710)
    at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:935)
    at android.view.Choreographer.doCallbacks(Choreographer.java:747)
    at android.view.Choreographer.doFrame(Choreographer.java:679)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:921)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6649)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)

这是我的代码:

public class UserDataActivity extends AppCompatActivity {

private RecyclerView datalist;
private DatabaseReference userRef;
private Toolbar mToolbar;

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

    mToolbar = findViewById(R.id.user_data_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("User Database");

    userRef = FirebaseDatabase.getInstance().getReference().child("users");
    userRef.keepSynced(true);
    datalist = findViewById(R.id.userdatalist);
    datalist.setHasFixedSize(true);
    datalist.setLayoutManager(new LinearLayoutManager(this));

}

@Override
protected void onStart() {
    super.onStart();

    final FirebaseRecyclerAdapter<userdata, DataViewHolder> firebaseRecyclerAdapter
            = new FirebaseRecyclerAdapter<userdata, DataViewHolder>(userdata.class, R.layout.userdata_layout, DataViewHolder.class, userRef) {

        @Override
        protected void populateViewHolder(DataViewHolder viewHolder, userdata model, final int position) {


            viewHolder.setEmail(model.getEmail());
            viewHolder.setName(model.getName());
            viewHolder.setPassword(model.getPassword());
            viewHolder.setPhone(model.getPhone());
            viewHolder.setSince(model.getSince());

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }


    };

    datalist.setAdapter(firebaseRecyclerAdapter);
}

public static class DataViewHolder extends RecyclerView.ViewHolder
{
    static View mView;

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

    public static void setName(String name)
    {
        TextView g = mView.findViewById(R.id.user_name);
        g.setText(name);
    }

    public static void setEmail(String email)
    {
        TextView g = mView.findViewById(R.id.user_email);
        g.setText(email);
    }

    public static void setPassword(String password)
    {
        TextView g = mView.findViewById(R.id.user_password);
        g.setText(password);
    }

    public static void setPhone(String phone)
    {
        TextView g = mView.findViewById(R.id.user_phone);
        g.setText(phone);
    }

    public static void setSince(String since)
    {
        TextView g = mView.findViewById(R.id.user_since);
        g.setText(since);
    }
}

}

userdata.class:

public class userdata {
public String email;
public String name;
public String password;
public String phone;
public String since;

public userdata(){}

public userdata(String email, String name, String password, String phone, String since) 
{
    this.email = email;
    this.name = name;
    this.password = password;
    this.phone = phone;
    this.since = since;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getSince() {
    return since;
}

public void setSince(String since) {
    this.since = since;
}

}

0 个答案:

没有答案