我首先要说我认为我想做的事情是不可能的(已经读过我认为是ISO C99的相关部分),但是这里是我现在所拥有的大致想法:
public class Help extends Fragment {
View myView;
Button btn;
TextView txtview;
private RecyclerView rview;
private RecyclerView.Adapter rviewadap;
ArrayList<MessagesModel> mm = new ArrayList<>();
public void initMessages() {
mm.add(new MessagesModel("Hello this is a test", "12:41"));
mm.add(new MessagesModel("This is another test", "12:43"));
mm.add(new MessagesModel("How are you doing today", "12:43"));
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myView = inflater.inflate(R.layout.help, container, false);
initMessages();
this.rview = myView.findViewById(R.id.reyclerview_message_list);
txtview = myView.findViewById(R.id.edittext_chatbox);
RecyclerView.LayoutManager layout = new LinearLayoutManager(this.getActivity());
this.rview.setLayoutManager(layout);
rviewadap = new MessagesAdapter(mm);
this.rview.setAdapter(rviewadap);
btn = myView.findViewById(R.id.button_chatbox_send);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mm.add(new MessagesModel(""+txtview.getText().toString(), "12:41"));
rviewadap.notifyDataSetChanged();
}
});
return myView;
}
public class MessagesModel{
String varMessage,varTime;
public MessagesModel(String varMessage,String varTime){
this.varMessage = varMessage;
this.varTime = varTime;
}
}
public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.ViewHolder> {
public ArrayList<MessagesModel> mm;
public MessagesAdapter(ArrayList<MessagesModel> mm) {
this.mm = mm;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = (View) LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_weather, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int i) {
viewHolder.message.setText(mm.get(i).varMessage);
viewHolder.time.setText(mm.get(i).varTime);
}
@Override
public int getItemCount() {
if(mm != null) {
return mm.size();
}else {
return 0;
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView message;
public TextView time;
public ViewHolder(@NonNull View view) {
super(view);
message = view.findViewById(R.id.day_txt);
System.out.println("body message id is: "+message);
time = view.findViewById(R.id.temp_txt);
}
}
}
}
这在C99 / C11中有效,但是我真正想要的是将name参数放到FOO_INITIALIZER宏中。有可能吗?
我知道这行不通:
struct foo {
int x;
int *y;
};
#define FOO_INITIALIZER(name) \
{ \
.x = 1, \
.y = &(name).x, \
}
struct foo bar = FOO_INITIALIZER(bar);
如果可以的话,我应该查看ISO / IEC 9899:TC3的哪个部分?
答案 0 :(得分:1)
无法用标准C语言实现所需的功能。您可以继续通过offsetof
传递名称(但不适用于嵌套对象)或存储偏移量而不是指针(在很大程度上取决于应用程序),而不是指针。