gpt4 book ai didi

Android - 将对象保存到对象列表

转载 作者:太空狗 更新时间:2023-10-29 14:08:32 24 4
gpt4 key购买 nike

我正在学习如何检索存储在 Parse 中的数据。我能够从 parse 中检索数据并将其绑定(bind)到简单的 ListView。现在我正在尝试模拟一个简单的聊天应用程序。我的解析类中有两列用于存储发件人姓名和聊天消息。我制作了一个自定义 ListView 来显示发件人姓名和消息,如下所示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/UserNameTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textColor="@color/material_blue_grey_900"
android:textSize="15sp" />
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/UserMessageTxt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5sp"
android:textColor="@color/material_blue_grey_900"
android:textSize="25sp" />
</LinearLayout>

和一个适配器类来绑定(bind)数据

public class ChatAdapter extends ArrayAdapter<Chat> {
Context context;
int layoutResourceId;
Chat data[] = null;
public ChatAdapter(Context context, int resource, Chat[] objects) {
super(context, resource, objects);
this.layoutResourceId=resource;
this.context=context;
this.data=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
ChatHolder holder=null;
if(row==null)
{
LayoutInflater inflater=((Activity)context).getLayoutInflater();
row=inflater.inflate(layoutResourceId,parent,false);
holder= new ChatHolder();
holder.UserName=(TextView)row.findViewById(R.id.UserNameTxt);
holder.UserMessage=(TextView)row.findViewById(R.id.UserMessageTxt);
row.setTag(holder);
}
else
{
holder=(ChatHolder)row.getTag();
}
Chat chat=data[position];
holder.UserName.setText(Chat.UserName);
holder.UserMessage.setText(Chat.UserMessage);
return row;
}
static class ChatHolder
{
TextView UserName;
TextView UserMessage;
}
}

聊天类是

public class Chat {
public static String UserName;
public static String UserMessage;
public Chat()
{
super();
}
public Chat(String _UserName,String _UserMessage)
{
super();
UserName=_UserName;
UserMessage=_UserMessage;
}
public String getName()
{
return UserName;
}
}

主要 Activity 的代码是(我刚刚包含了重要部分)

List<Chat> chatData=new ArrayList<Chat>();

@Override
protected Void doInBackground(Void... params) {
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
"Chat");
try {
ob = query.find();
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void result) {
Chat c;
listview = (ListView) findViewById(R.id.listView);
for (ParseObject country : ob) {
chatData.add(new Chat((String) country.get("Sender"),(String) country.get("ChatMessage")));
}
String name01=chatData.get(0).getName(); //for debugging
String name02=chatData.get(1).getName(); //for debugging
String name03=chatData.get(2).getName(); //for debugging

Chat chatArray[]=chatData.toArray(new Chat[chatData.size()]);

String name=chatArray[0].getName(); //for debugging
String name1=chatArray[1].getName(); //for debugging
String name2=chatArray[2].getName(); //for debugging

ChatAdapter cAdapter=new ChatAdapter(MainActivity.this,R.layout.listview_item,chatArray);

listview.setAdapter(cAdapter);
mProgressDialog.dismiss();
}

输出截图 http://i.stack.imgur.com/bxnto.png

我的 Parse 类中有三行数据。作为输出,我只得到最后插入的行 3 次。这是调试Windows的截图http://i.stack.imgur.com/l7CAs.png

在 for 循环中,我从解析中获取了正确的值,但在将其添加到 chatData 列表后,发生了一些错误。

这是我的第一个问题。对于这个大帖子,如果我不清楚这个问题,我感到非常抱歉。谢谢你

最佳答案

在我看来,您的 Chat 类中的 static UserNameUserMessage 是问题的原因。将它们更改为

public String UserName;    //from  public static String UserName;
public String UserMessage; //from public static String UserMessage;

并在您的适配器中更改此部分:

holder.UserName.setText(chat.UserName);       // from Chat.UserName
holder.UserMessage.setText(chat.UserMessage); // from Chat.UserMessage

关于Android - 将对象保存到对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30922129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com