我制作了一个简单的应用程序,显示校园周围发生的当地事件。每个活动都在Cardview中保存,看起来像this。红色方块是图标的占位符,用于显示此类事件。
我想更改ImageView(红色方块),具体取决于值子字符串的值。因此,如果孩子的字符串值"类别"是"音乐",将显示音符图标而不是红色方块等。我知道我可以使用switch语句手动读取该子项的值,然后设置ImageView基于字符串值,但我不知道将它放在我的代码中的位置,或者这是否是最好的方法。您有什么推荐的吗?这是片段的代码:
public class EventsFragment extends Fragment {
public String postKey;
public static class EventViewHolder extends RecyclerView.ViewHolder{
public TextView eventTitle;
public ImageView eventImage;
public ImageView eventCategory;
public TextView eventDate;
View mView;
public EventViewHolder(View v){
super(v);
mView = v;
eventTitle = (TextView)mView.findViewById(R.id.title);
eventImage = (ImageView)mView.findViewById(R.id.image);
eventCategory = (ImageView)mView.findViewById(R.id.category);
eventDate = (TextView)mView.findViewById(R.id.date);
}
public TextView getEventTitle() {
return eventTitle;
}
public void setEventTitle(TextView eventTitle) {
this.eventTitle = eventTitle;
}
}
private DatabaseReference mFirebaseDatabaseReference;
private FirebaseRecyclerAdapter<ModelClass, EventViewHolder> mFirebaseAdapter;
private RecyclerView mEventRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private static String TAG = "EventsFragment";
public static final String DATA = "Data";
//VARIABLES
private RecyclerView mBlogList;
private FirebaseDatabase database;
private DatabaseReference ref;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.events_fragment,container,false);
v.setTag(TAG);
//RV
mEventRecyclerView = (RecyclerView)v.findViewById(R.id.events_list);
//LLM
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mEventRecyclerView.setLayoutManager(mLinearLayoutManager);
//DB
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
mFirebaseAdapter = new FirebaseRecyclerAdapter<ModelClass, EventViewHolder>(
ModelClass.class,
R.layout.design_row,
EventViewHolder.class,
mFirebaseDatabaseReference.child(DATA))
{
@Override
protected void populateViewHolder(EventViewHolder viewHolder, ModelClass model, final int position) {
final String post_key = getRef(position).getKey();
viewHolder.eventDate.setText(model.getDate());
viewHolder.eventTitle.setText(model.getTitle());
Picasso.with(getActivity().getApplicationContext())
.load(model.getImage())
.fit()
.into(viewHolder.eventImage);
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setPostKey(post_key);
mFirebaseDatabaseReference.child(DATA).child(post_key).addValueEventListener(new ValueEventListener() {
String post_info, post_title, post_image, post_category;
@Override
public void onDataChange(DataSnapshot snapshot) {
System.out.println(snapshot.getValue());
post_info = snapshot.child("info").getValue().toString();
post_title = snapshot.child("title").getValue().toString();
post_image = snapshot.child("image").getValue().toString();
post_category = snapshot.child("category").getValue().toString();
Bundle bundle = new Bundle();
bundle.putString("info", post_info);
bundle.putString("title", post_title);
bundle.putString("image", post_image);
FragmentManager fragM = getActivity().getSupportFragmentManager();
FragmentTransaction fragT = fragM.beginTransaction();
EventsExpandedFragment expand = new EventsExpandedFragment();
expand.setArguments(bundle);
fragT.replace(R.id.frame, expand);
fragT.addToBackStack("");
fragT.commit();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
};
mEventRecyclerView.setLayoutManager(mLinearLayoutManager);
mEventRecyclerView.setAdapter(mFirebaseAdapter);
return v;
}
public String getPostKey() {
return postKey;
}
public void setPostKey(String postKey) {
this.postKey = postKey;
}
}
这是一个完整的截图供参考:
答案 0 :(得分:0)
我认为你应该把switch语句放在populateViewHolder
@Override
protected void populateViewHolder(EventViewHolder viewHolder, ModelClass model, final int position) {
switch (model.eventCategory) {
case "eventType1": {
//load your event category image
}
case "eventType2": {
//load your event category image
}
default:
break;
}
// other actions
}
这回答你的问题吗?