【发布时间】:2012-08-11 13:12:03
【问题描述】:
我有一个自定义的 ArrayAdapter,用于一个 android 聊天应用程序中的 listView。我在 editText 组件中输入了一些文本,然后在按下按钮时提交文本。
问题是,当我的自定义适配器中的 getView 方法被调用时,整个列表都会被刷新,并且我列表中的每一行都与最后提交的信息相同。
有人知道我如何在 listView 中使用自定义 ArrayAdapter 和其他行添加一行以获得原始文本吗?
这就是我调用适配器的方式:
m_discussionThread = new ArrayList<String>();
m_discussionThreadAdapter = new ChatListAdapter(this, m_discussionThread);
list.setAdapter(m_discussionThreadAdapter);
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String text = message.getText().toString();
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text);
m_connection.sendPacket(msg);
m_discussionThread.add(text);
runOnUiThread(new Runnable() {
@Override
public void run() {
m_discussionThreadAdapter.notifyDataSetChanged();
}
});
}
});
这是适配器:
public ChatListAdapter(Context context, ArrayList<String> chatValue) {
super(context,R.layout.list_row_layout_even, chatValue);
this.context = context;
this.chatValue = chatValue;
}
@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.list_row_layout_even, parent, false);
TextView chatText = (TextView) rowView.findViewById(R.id.chat_message);
chatText.setText(chatValue.get(chatValue.size()-1).toString());
return rowView;
【问题讨论】:
-
您的代码中似乎存在错误。为了让我们帮助您,我们(嗯,至少我)将需要您的适配器代码
-
感谢您添加代码 :)
-
感谢您的回答。它有效!
标签: android android-layout android-listview