过去的几天我在这个问题上花费时间。 我正在尝试基于https://blog.sendbird.com/android-chat-tutorial-building-a-messaging-ui
实现聊天应用不知何故,我收到“未连接适配器,跳过布局”错误。我相信我在活动中的代码是正确的。我也知道msgidlist不为空。 在我的代码中,我应该看到一些基于在凌空url上获取的json数据的示例会话。
看起来像: [{“ msgid”:“ 280780”,“ id_from”:“ 2243”,“ id_to”:“ 5073”,“ sender”:“ Morgany”,“ receiver”:“ Frederic_SmartVibes”,“ body”:“ super,weer een testbericht。En weer zonder kusje。Op ons 6 Jarige jubileum nog wel ...“,”时间戳“:” 2018-08-17 22:05:06“,”已读“:” 1“},{” msgid “:” 279698“,” id_from“:” 5073“,” id_to“:” 2243“,”发送者“:” Frederic_SmartVibes“,”接收者“:” Morgany“,” body“:” Dit is een testberichtje gericht aan jou “,” timestamp“:” 2018-08-14 15:42:20“,” wasread“:” 1“},{” msgid“:” 280882“,” id_from“:” 5073“,” id_to“:” 2243“,”发送者“:” Frederic_SmartVibes“,”接收者“:”摩根“,”身体“:” kus“,”时间戳“:” 2018-08-18 01:47:35“,” wasread“:” 1 “},{” msgid“:” 298502“,” id_from“:” 5073“,” id_to“:” 2243“,” sender“:” Frederic_SmartVibes“,” receiver“:” Morgany“,” body“:” iets liefs“,” timestamp“:” 2018-10-17 16:02:32“,” wasread“:” 1“},{” msgid“:” 298503“,” id_from“:” 5073“,” id_to“: “ 2243”,“发送者”:“ Frederic_SmartVibes”,“接收者”:“摩根”,“身体”:“ noooo”,“时间戳”:“ 2018-10-17 16:02:43”,“已读”:“ 1“}]
活动代码:
public class MessageListActivity extends AppCompatActivity {
public RecyclerView mMessageRecycler;
public MessageListAdapter mMessageAdapter;
ArrayList<String> msgidlist = new ArrayList<String>();
RequestQueue rq;
String url, id_from, id_to, msgid, m_from, m_to, mnicksender, mnickreceiver, mbody,mtimestamp, mwasread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_list);
mMessageRecycler = findViewById(R.id.reyclerview_message_list);
mMessageRecycler.setHasFixedSize(true);
mMessageRecycler.setLayoutManager(new LinearLayoutManager(MessageListActivity.this));
id_to="2243";
id_from="5073";
url = "https://www.someurl.com/somescript.php?id_from="+id_from+"&id_to="+id_to;
rq = Volley.newRequestQueue(this);
sendjsonrequest();
}
public void sendjsonrequest() {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
// Process the JSON
try{
// Loop through the array elements
for(int i=0;i<response.length();i++){
// Get current json object
JSONObject messages = response.getJSONObject(i);
// Get the current profile (json object) data
msgid = messages.getString("msgid");
m_from = messages.getString("id_from");
m_to = messages.getString("id_to");
mnicksender = messages.getString("sender");
mnickreceiver = messages.getString("receiver");
mbody = messages.getString("body");
mtimestamp = messages.getString("timestamp");
mwasread = messages.getString("wasread");
messageData.setmsgid(msgid);
messageData.setm_from(m_from);
messageData.setm_to(m_to);
messageData.setmnicksender(mnicksender);
messageData.setMnickreceiver(mnickreceiver);
messageData.setBody(mbody);
messageData.setTimestamp(mtimestamp);
messageData.setWasread(mwasread);
msgidlist.add(msgid);
}
}catch (JSONException e){
e.printStackTrace();
}
mMessageAdapter = new MessageListAdapter(MessageListActivity.this, msgidlist);
mMessageRecycler.setAdapter(mMessageAdapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
rq.add(jsonArrayRequest);
}
}
我的适配器:
public class MessageListAdapter extends RecyclerView.Adapter {
private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;
private Context mContext;
private ArrayList<String> mMessageList;
public String myid = "5073"; //CHANGE THIS
public MessageListAdapter(Context context, ArrayList<String> messageList) {
mContext = context;
mMessageList = messageList;
}
@Override
public int getItemCount() {
return mMessageList.size();
}
// Determines the appropriate ViewType according to the sender of the message.
@Override
public int getItemViewType(int position) {
//UserMessage message = (UserMessage) mMessageList.get(position);
if (messageData.getFrom(position).equals(myid)) {
// If the current user is the sender of the message
return VIEW_TYPE_MESSAGE_SENT;
} else {
// If some other user sent the message
return VIEW_TYPE_MESSAGE_RECEIVED;
}
}
// Inflates the appropriate layout according to the ViewType.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_sent, parent, false);
return new SentMessageHolder(view);
} else if (viewType == VIEW_TYPE_MESSAGE_RECEIVED) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message_received, parent, false);
return new ReceivedMessageHolder(view);
}
return null;
}
// Passes the message object to a ViewHolder so that the contents can be bound to UI.
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
String message = (String) mMessageList.get(position);
switch (holder.getItemViewType()) {
case VIEW_TYPE_MESSAGE_SENT:
((SentMessageHolder) holder).bind(message);
break;
case VIEW_TYPE_MESSAGE_RECEIVED:
((ReceivedMessageHolder) holder).bind(message);
}
}
private class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText;
SentMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
}
void bind(String message) {
messageText.setText(messageData.getBody(Integer.parseInt(message)));
// Format the stored timestamp into a readable String using method.
timeText.setText(messageData.getTimestamp(Integer.parseInt(message)));
}
}
private class ReceivedMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
ImageView profileImage;
ReceivedMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
nameText = (TextView) itemView.findViewById(R.id.text_message_name);
profileImage = (ImageView) itemView.findViewById(R.id.image_message_profile);
}
void bind(String message) {
messageText.setText(messageData.getBody(Integer.parseInt(message)));
// Format the stored timestamp into a readable String using method.
timeText.setText(messageData.getTimestamp(Integer.parseInt(message)));
nameText.setText(messageData.getreceiver(Integer.parseInt(message)));
// Insert the profile image from the URL into the ImageView.
//Utils.displayRoundImageFromUrl(mContext, message.getSender().getProfileUrl(), profileImage);
}
}
}
短信持有人:(接收的短信持有人基本相同)
class SentMessageHolder extends RecyclerView.ViewHolder {
TextView messageText, timeText, nameText;
ImageView profileImage;
SentMessageHolder(View itemView) {
super(itemView);
messageText = (TextView) itemView.findViewById(R.id.text_message_body);
timeText = (TextView) itemView.findViewById(R.id.text_message_time);
}
void bind(int message) {
messageText.setText(messageData.getBody(message));
// Format the stored timestamp into a readable String using method.
timeText.setText(messageData.getTimestamp(message));
//nameText.setText(message.getSender().getNickname());
// Insert the profile image from the URL into the ImageView.
//Utils.displayRoundImageFromUrl(mContext, message.getSender().getProfileUrl(), profileImage);
}
}
任何帮助将不胜感激。 (我是Android Studio的新手)