notifyDataSetChanged()不刷新listview

时间:2014-03-29 16:39:04

标签: android listview adapter 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来编写消息时,它会刷新视图并显示消息。 我希望它一出现就会出现。 请帮帮我。

1 个答案:

答案 0 :(得分:1)

删除

public void addMessage(String msg) {
log.add(msg);
notifyDataSetChanged();

并在您的主要活动中使用它:

Conversa.log.add(msg);
notifyDataSetChanged();