qq_37853991
2017-05-11 09:07
采纳率: 100%
浏览 1.4k
已采纳

listview无法显示,求大神解决

适配器
class MsgAdapter extends BaseAdapter {

    private List<Msg> listMsg = new ArrayList<Msg>();

    private Context cxt;
    private LayoutInflater inflater;

    public MsgAdapter(ChatActivity ChatActivity) {
            this.cxt = ChatActivity;
    }



    @Override
    public int getCount() {
            return listMsg.size();
    }

    @Override
    public Object getItem(int position) {
            return listMsg.get(position);
    }

    @Override
    public long getItemId(int position) {
            return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            if(listMsg.get(position).from.equals("IN")){
                    this.inflater = (LayoutInflater) this.cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = this.inflater.inflate(R.layout.msg_left, null);
            }else{
                    convertView = this.inflater.inflate(R.layout.msg_right, null);
            }
            TextView useridView = (TextView) convertView.findViewById(R.id.userid);
            TextView dateView = (TextView) convertView.findViewById(R.id.date);
            TextView msgView = (TextView) convertView.findViewById(R.id.msg);
            useridView.setText(listMsg.get(position).userid);
            dateView.setText(listMsg.get(position).date);
            msgView.setText(listMsg.get(position).msg);
            return convertView;
    }

}

消息类
public class Msg {
String userid;
String msg;
String date;
String from;

    public Msg(String userid, String msg, String date, String from) {
            this.userid = userid;
            this.msg = msg;
            this.date = date;
            this.from = from;
    }

}

主程序
public class ChatActivity extends Activity{

    private ListView msgListView;
    private EditText inputText;
    private MsgAdapter adapter;
    private Button send;
    private List<Msg> listMsg=new ArrayList<Msg>();
    private String pUSERID="用户";
    String strFirendName = "医生";
    String user = "test1";


    @Override
    protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.chat);

    //initMsgs();

    inputText=(EditText)findViewById(R.id.chat_edit);
    send=(Button)findViewById(R.id.send);
    adapter = new MsgAdapter(this);
    msgListView=(ListView)findViewById(R.id.listview);

    msgListView.setAdapter(adapter);
    ChatManager cm = connect.getConnection().getChatManager();
            cm.addChatListener(new ChatManagerListener() {
                    @Override
                    public void chatCreated(Chat chat, boolean able) {
                            chat.addMessageListener(new MessageListener() {
                                    @Override
                                    public void processMessage(Chat chat2, Message message) {
                                            //message from user [test2@hp-pc]
                                                    String[] args = new String[] {strFirendName , message.getBody(), TimeRender.getDate(), "IN" };
                                                    android.os.Message msg = handler.obtainMessage();

                                                    msg.obj = args;
                                                    msg.sendToTarget();
                                                    System.out.println(message.getBody());
                                                    // orther user / group / admin of the openfire
                                                    // do work...
                                    }
                            });
                    }
            });
    //发送

            final Chat newchat = cm.createChat( user + "@hp-pc", null);
    send.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                            String msg = inputText.getText().toString();
                            if(msg.length() > 0){

                                    listMsg.add(new Msg(pUSERID, msg, TimeRender.getDate(), "OUT"));
                                    Log.e("ChatActivity","addmsg");
                                    adapter.notifyDataSetChanged();
                                    Log.e("ChatActivity","change");
                                    try {
                                            newchat.sendMessage(msg);
                                    } catch (XMPPException e) {
                                            e.printStackTrace();
                                    }
                            }
                            inputText.setText("");
                    }
            });


            }

    private Handler handler = new Handler() {
            public void handleMessage(android.os.Message msg) {

                            String[] args = (String[]) msg.obj;
                            listMsg.add(new Msg(args[0], args[1], args[2], args[3]));
                            //动态刷新
                            adapter.notifyDataSetChanged();

                    }
            };
    };

是一个聊天界面的listview,测试的时候用spark收得到消息,但是自己的界面就收发都不显示,不知道为什么,网上查了也不知道问题

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • -无-怨-无-悔- 2017-05-11 09:11
    已采纳

    初始化时你的listMsg没有传到MsgAdapter 里面

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • 下课睡觉 2017-05-11 09:11

    你这List数据都没有放到adapter里面去啊,notifyDataSetChanged有什么用?

    评论
    解决 无用
    打赏 举报
  • 昵称_Justin 2017-05-11 09:41

    加个有参不就什么都解决了,写的太麻烦了,这样写后面会出现很多错,而且还没用复用机制,数据一多会占用好多内存

    评论
    解决 无用
    打赏 举报
  • 你也很优秀 2017-05-11 09:52

    MsgAdapter里面没有添加数据

    评论
    解决 无用
    打赏 举报
  • huohao-nicole 2017-06-08 02:04

    listview写的差点意思了吧,楼主应该先好好学学listivew的写法(item应该进行复用的),如果数据特别多的话,那内存肯定会时一个问题,做开发想要进阶就要既知其然也要知其所以然
    你要知道为什么要把数据的集合加入到构造函数中

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题