我在listview
中为Android
设置适配器时遇到了一个奇怪的错误。我正在开发一个聊天应用程序,当我发送它出现在列表视图中的任何消息时,但是当我尝试接收消息时,它会带我到Catch-block
类FutureTask
我正在使用adapter.notifyDataSetChanged();
让我的适配器知道我在适配器中创建的任何更改。
我的代码如下
private void addNewMessage(MessageItems m) {
if(MainActivity.mymessage)
{
messages.add(m);
}else if(MainActivity.mymessage == false)
{
messages.add(m);
}
adapter.notifyDataSetChanged();
getListView().setSelection(messages.size()-1);
}
**Code of Adapter**
public class AwesomeAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<MessageItems> mMessages;
public AwesomeAdapter(Context context, ArrayList<MessageItems> messages) {
super();
this.mContext = context;
this.mMessages = messages;
}
@Override
public int getCount() {
return mMessages.size();
}
@Override
public Object getItem(int position) {
return mMessages.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MessageItems message = (MessageItems) this.getItem(position);
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.message_text);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.message.setText(message.getMessage());
LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
//check if it is a status message then remove background, and change text color.
if(message.isStatusMessage())
{
holder.message.setBackgroundDrawable(null);
lp.gravity = Gravity.LEFT;
holder.message.getResources().getColor(R.color.textFieldColor);
//holder.message.setTextColor(R.color.textFieldColor);
}
else
{
//Check whether message is mine to show green background and align to right
if(message.isMine())
{
holder.message.setBackgroundResource(R.drawable.speech_bubble_green);
lp.gravity = Gravity.RIGHT;
}
//If not mine then it is from sender to show orange background and align to left
else
{
holder.message.setBackgroundResource(R.drawable.speech_bubble_orange);
lp.gravity = Gravity.LEFT;
}
holder.message.setLayoutParams(lp);
holder.message.getResources().getColor(R.color.textFieldColor);
//holder.message.setTextColor(R.color.textColor);
}
return convertView;
}
private static class ViewHolder
{
TextView message;
}
@Override
public long getItemId(int position) {
//Unimplemented, because we aren't using Sqlite.
return position;
}
在FutureTask类中,当我尝试设置adapter.notifyDataSetChanged();
时,它会将我带到以下try-catch块public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
我知道某个地方的问题在于为listview设置适配器,我需要在收到新消息时更新它,但我无法修复它。感谢您的帮助