我现在被困住了:
我有一个带有CardViews列表的RecyclerView。
按下按钮时,addItem();方法被调用,显然会添加一个项目。
方法如下:
String text = hwtext.getText().toString();
mList.add(new HomeWork_Item(the.image.I.want, text));
mAdapter.notifyItemInserted(mList.size());
哪个作品很棒!
现在,我添加了一个微调器。微调框包含一些主题。假设您选择了主题历史记录,那么我想将the.image.I.want
的方法从addItem();
更改为可绘制对象中的另一个方法。
我尝试使用滑行功能,但这不起作用:
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String SpinnerText = parent.getItemAtPosition(position).toString();
Toast.makeText(getContext(), SpinnerText, Toast.LENGTH_SHORT).show();
if (SpinnerText.equals("History")) {
Glide.with(getActivity()).load(R.drawable.history).into(R.id.theImageView);
}
}
注意:仅当按下按钮调用该项目的添加方法时,该项目才会添加。选择主题后,应该准备要在添加项目时使用的图像。
谢谢!
崩溃Logcat:
2019-03-08 14:59:10.328 9808-9808/com.reogen.ssr E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.reogen.ssr, PID: 9808
java.lang.NullPointerException: Argument must not be null
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:31)
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:25)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:685)
at com.reogen.ssr.Fragments.Homework.onItemSelected(Homework.java:170)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:1331)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1320)
at android.widget.AdapterView.-wrap1(Unknown Source:0)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1282)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:0)
创建一个ImageView
为
ImageView historyImage = findViewById(R.id.historyImage);
然后在滑行中执行此操作:
Glide.with(view.getContext()).load(R.drawable.history).into(historyImage);
您要将int传递给into
,应该直接传递ImageView
。
将您的代码更改为此
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_homework, container, false);
insert = rootView.findViewById(R.id.insert_text);
hwtext = rootView.findViewById(R.id.hwinput);
SubjectImage = rootView.findViewById(id.HWImage);
spinner = rootView.findViewById(R.id.spinner);
buildView(rootView);
return rootView;
}
public void buildView(View rootView) {
mRecyclerView = rootView.findViewById(R.id.RecycleHW);
mRecyclerView.setHasFixedSize(true);
mManager = new LinearLayoutManager(getActivity());
mAdapter = new HomeWorkAdapter(mList);
mRecyclerView.setLayoutManager(mManager);
mRecyclerView.setAdapter(mAdapter);
}