所以情况是我不想从片段中将对象添加到RecyclerView,我知道该怎么做。但是,添加对象时不显示数据。这是我的代码:
ListAdapter:
public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public static final int ITEM_TYPE_NOTE = 0;
public static final int ITEM_TYPE_CATEGORY = 1;
private Context mContext;
private List<ObjectModel> mList;
public ListAdapter(Context context, List<ObjectModel> list) {
mContext = context;
mList = list;
}
@Override
public int getItemViewType(int position) {
return position % 2 * 2;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View noteView = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_list_item, parent, false);
View categoryView = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_list_item, parent, false);
switch (viewType) {
case ITEM_TYPE_NOTE:
return new NoteViewHolder(noteView);
case ITEM_TYPE_CATEGORY:
return new CategoryViewHolder(categoryView);
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case ITEM_TYPE_NOTE:
NoteViewHolder noteViewHolder = (NoteViewHolder) holder;
noteViewHolder.bindNote((Note) mList.get(position));
break;
case ITEM_TYPE_CATEGORY:
CategoryViewHolder categoryViewHolder = (CategoryViewHolder) holder;
categoryViewHolder.bindCategory((Category) mList.get(position));
break;
}
}
@Override
public int getItemCount() {
return 0;
}
class NoteViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
public ImageView mNoteListItemImage;
public TextView mNoteListItemText;
public ImageView mDeleteNoteImage;
public NoteViewHolder(View itemView) {
super(itemView);
mNoteListItemImage = (ImageView) itemView.findViewById(R.id.NoteListItemImage);
mNoteListItemText = (TextView) itemView.findViewById(R.id.NoteListItemText);
mDeleteNoteImage = (ImageView) itemView.findViewById(R.id.DeleteNoteImage);
itemView.setOnClickListener(this);
}
public void bindNote(Note note) {
mNoteListItemText.setText(note.getTitle());
}
@Override
public void onClick(View view) {
}
}
主要活动:
public class MainActivity extends AppCompatActivity {
private ImageView mAddItemImage;
private RecyclerView mRecyclerView;
private Note mNote;
private List<ObjectModel> mList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAddItemImage = (ImageView) findViewById(R.id.AddItemImage);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mAddItemImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startNewItemActivity();
}
});
Intent intent = getIntent();
mNote = (Note) intent.getSerializableExtra(getString(R.string.note));
mList.add(mNote);
ListAdapter listAdapter = new ListAdapter(this, mList);
mRecyclerView.setAdapter(listAdapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
}
编辑5 - 禁用即时运行并获得此信息:
09-28 10:54:22.725 9992-9992/com.x.x E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.x.x, PID: 9992
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.x.x.items.Note.getTitle()' on a null object reference
at com.x.x.adapters.ListAdapter$NoteViewHolder.bindNote(ListAdapter.java:88)
at com.x.x.adapters.ListAdapter.onBindViewHolder(ListAdapter.java:56)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15654)
at android.view.ViewGroup.layout(ViewGroup.java:4969)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2102)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1859)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1078)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5875)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5351)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
请帮忙!
更新
这就是我将信息传递到我的列表的方式 -
在我创建新笔记的片段中,有两个EditTexts。然后将EditTexts中的信息传递给MainActivity。
@Override
public void onClick(View v) {
if (v == mConfirmImageNN) {
String titleText = mNoteTitleEdit.getText().toString();
String descriptionText = mNoteDescriptionEdit.getText().toString();
// Display an error if both EditTexts are empty
if (v == mConfirmImageNN && titleText.equals("")) {
noteError();
} else if (v == mConfirmImageNN && descriptionText.equals("")){
noteError();
} else {
// Create note otherwise
Note note = new Note(titleText, descriptionText);
goBackNote(note);
}
}
// Go back to MainActivity with no info
if (v == mCancelImageNN) {
goBackNone();
}
}
// Go back to MainActivity
private void goBackNote(Note note) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra(getString(R.string.note), (Serializable) note);
startActivity(intent);
}
// With nothing
private void goBackNone() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
我使用捆绑包检索信息。
Bundle bundle = getIntent().getExtras();
mNote = (Note) bundle.getSerializable(getString(R.string.note));
然后我将它添加到我的列表中。
mList.add(mNote);
另一方面,这是我初始化列表的方式。
private List<ObjectModel> mList = new ArrayList<>();
ObjectModel。
public class ObjectModel {
Note mNote;
Category mCategory;
}
答案 0 :(得分:0)
我认为您有两个错误
@Override
public int getItemViewType(int position) {
return position % 2 * 2;
}
在这里,您允许 ViewType = 0,1和2的项目。如果您获得的值不同于0或1,则返回 null 。
记住运算符如何处理优先级。
position % 2 * 2
将与
一样评估(position % 2) * 2
我认为你想要的是
@Override
public int getItemViewType(int position) {
return position % 2;
}
此外,您将使用值0覆盖getItemCount方法。这意味着RecyclerView上没有项目,因此不会在那里呈现任何内容。
@Override
public int getItemCount() {
return 0;
}
使用列表及其大小(如
)更改零值...
private List<Items> items;
...
@Override
public int getItemCount() {
return items.size();
}
答案 1 :(得分:0)
@Override
public int getItemCount() {
return 0;
}
到
@Override
public int getItemCount() {
return mList.size();
}