0
這是我的適配器:notifyDataSetChanged()不刷新列表視圖
public class Conversa extends BaseAdapter {
private Context context;
private ArrayList<String> log;
private String target;
public Conversa(Context context, String target) {
this.context = context;
this.target = target;
log = new ArrayList<String>();
}
@Override
public int getCount() {
return log.size();
}
@Override
public Object getItem(int pos) {
return log.get(pos);
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.msg_layout, parent, false);
}
TextView msg = (TextView) convertView.findViewById(R.id.msg);
msg.setText(log.get(pos));
return convertView;
}
public ArrayList<String> getLog() {
return log;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public void addMessage(String msg) {
log.add(msg);
notifyDataSetChanged();
}
}
這是我的片段,它富人當Conversa.addMessage()被調用未刷新列表視圖。
public class ChatFragment extends Fragment {
private String target;
private ListView listview;
private EditText edittext;
private Conversa conversa;
private MainActivity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
target = getArguments().getString(IRCService.EXTRA_TARGET);
activity = (MainActivity) getActivity();
conversa = activity.service.getConversa(target);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
View view;
view = inflater.inflate(R.layout.chat_layout, container, false);
edittext = (EditText) view.findViewById(R.id.chatinput);
listview = (ListView) view.findViewById(R.id.chatlist);
listview.setAdapter(conversa);
// EM CASO DE RETORNO DO SERVICE EM BACKGROUND, ROLAR A LISTA ATEH O FIM
if (!conversa.getLog().isEmpty()) {
scrollMyListViewToBottom();
}
// LISTENER QUE RECEBERA O "ENVIAR" DO TECLADO DO ANDROID
edittext.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
// ESCONDE O TECLADO APOS ENVIAR
InputMethodManager in = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(
edittext.getApplicationWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
sendMessage(target, edittext.getText().toString());
// ALTERA O FLAG
handled = true;
}
return handled;
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (target.equals(IRCService.CANAL)) {
inflater.inflate(R.menu.canal_menu, menu);
} else {
inflater.inflate(R.menu.pvt_menu, menu);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_closepvt:
activity.removePVTTab(conversa.getTarget());
return true;
case R.id.menu_desconectar:
activity.service.IRCdisconnect();
return true;
case R.id.menu_mudarnick:
// TODO IMPLEMENTAR MUDANÇA DE NICK
return true;
}
return false;
}
public void sendMessage(String target, String msg) {
activity.service.sendMessage(target, msg);
// APAGA O EDITTEXT
edittext.setText("");
}
private void scrollMyListViewToBottom() {
listview.post(new Runnable() {
@Override
public void run() {
// Select the last row so it will scroll into view...
listview.setSelection(conversa.getCount() - 1);
}
});
}
public void setQuote(String nick) {
((MainActivity) getActivity()).drawerlayout.closeDrawers();
edittext.setText(nick + ": ");
edittext.requestFocus();
}
}
如果我收到一條聊天消息,它不會出現在列表上,儘管消息存在於日誌中。當我觸摸edittext編寫消息時,它刷新視圖並顯示消息。 我想它一出現即會出現。 請幫幫我。
notifyDataSetChanged必須從UI線程調用。 – Skynet
@Matheus Henrique da Silva如果答案是有用的,請接受或/和upvote它:-))) –
我的notifyDataSetChanged()正在守護線程上運行,我該如何使它在UI線程上運行? –