【发布时间】:2020-12-29 14:15:18
【问题描述】:
我正在尝试用一个显示其外观的 xml 文件和一个可以从中获取消息文本的类来填充一个 xml(聊天日志)。
我正在使用 ArrayAdapter 和通货膨胀。我有 2 个 XML 文件,其中一个是“messageother”,用于其他人的消息,“messageme”用于用户自己编写的消息。
public class MsgAdapter extends ArrayAdapter<Msg> {
private final Context context;
private final ArrayList<Msg> msgList;
private TextView tvText;
public MsgAdapter(Context context, ArrayList<Msg> messages) {
super(context, R.layout.activ, messages);
this.context=context;
this.msgList=messages;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.messageother, parent, false);
return rowView;
}
}
现在,这只会复制“messageother”的相同 xml 文件,直到完成。 相反,id 喜欢在 ListView 中一一查看,并检查 Msg.getMine() = true; 如果是这样,我想用 xml “messageme” 像这样膨胀那个:
View rowView = inflater.inflate(R.layout.messageme, parent, false);
我怎么能做这样的事?
【问题讨论】:
标签: java android xml layout-inflater