如何在刷新ListView后从下到上添加项目?

时间:2015-02-17 09:02:20

标签: android

这是我在Android中打印列表视图的课程

 public class ChatActivity extends FragmentActivity implements OnClickListener {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.act_chat);
            listview = (PullToRefreshListView) findViewById(R.id.listView);
            editMsg = (EditText) findViewById(R.id.txt_inputText);
            mLastSeenTime = (TextView) findViewById(R.id.lastSeenDatetextview);
            sendMsgBut = (ImageView) findViewById(R.id.btn_Send);
            recciverImage = (ImageView) findViewById(R.id.recciverImage);
            backbutton = (ImageView) findViewById(R.id.contact_btnMenu);
            sendMsgBut.setOnClickListener(this);
            recciverImage.setOnClickListener(this);
            backbutton.setOnClickListener(this);    


        arrChatlist = dbHelper.fetchChatHistory(member_id, reccvierid);
            chatList.addAll(arrChatlist);
            chatAdapter = new ChatAdapter(this, chatList);
            listview.setAdapter(chatAdapter);
            listview.setSelection(listview.getAdapter().getCount() - 1);
            dbHelper.updateIsRead(reccvierid);

        @Override
        public void onClick(View arg0) {
            switch (arg0.getId()) {
            case R.id.btn_Send:
                if (TextUtils.isEmpty(editMsg.getText().toString()))
                    return;
                else {
                    mChatService.SendMessage(Integer.parseInt(member_id), Integer
                            .parseInt(reccvierid), editMsg.getText().toString());
                    Chat chat = new Chat();
                    chat.setMessage(editMsg.getText().toString());
                    chat.setRecieverID(reccvierid);
                    chat.setSenderID(member_id);
                    chatList.add(chat);
                    dbHelper.addMessage(chat);
                    chatAdapter.notifyDataSetChanged();
                    editMsg.setText("");
                    System.out.println("Messae : " + chat.getMessage());
                    listview.setSelection(listview.getAdapter().getCount() - 1);
                }
                break;



            default:
                break;
            }
        }


        // ****************************** For Location Name *********|||||||


        private class SyncStateReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {
                switch (intent.getIntExtra(BroadcastNotifier.EXTENDED_DATA_STATUS,
                        BroadcastNotifier.STATE_ACTION_COMPLETE)) {
                case BroadcastNotifier.STATE_ACTION_MESSAGE_RECEVED:
                    boolean isCurrentUser = false;
                    Chat chat = (Chat) intent
                            .getSerializableExtra(BroadcastNotifier.EXTENDED_CHAT_RECEIVED);
                    for (Datamodel model : HomeActivity.arrModel) {
                        if (("~" + model.getPhoneNumber()).equals(chat
                                .getSenderName())) {
                            chat.setSenderID(model.getId());
                            chat.setSenderName(model.getContactName());
                        }
                        if (chat.getConnectionId().equals(ConnectionId)) {
                            isCurrentUser = true;
                        }
                    }
                    if (isCurrentUser) {
                        dbHelper.addMessage(chat);
                        chatList.add(chat);
                        chatAdapter.notifyDataSetChanged();
                        listview.setSelection(listview.getAdapter().getCount() - 1);
                    } else {
                        mChatService.createNotification(chat);
                    }
                    break;
                }
            }

        }


    //-------- SigNal R Method------------------------\\\
        public ChatEventHandler mChatHandler = new ChatEventHandler() {
            String previousMessage = "";
            @Override
            public void UpdateMessage(String message, String sendername,
                    long senderId) {
                if (previousMessage != message) {
                    System.out.println("new message: " + message + " sendername:"
                            + sendername);
                    Chat chat = new Chat();
                    chat.setMessage(message);
                    chat.setRecieverID(member_id);
                    chat.setSenderID("" + senderId);
                    chat.setSenderName(sendername);
                    chat.setIsUnread(false);
                    dbHelper.addMessage(chat);
                    chatList.add(chat);

                    previousMessage = message;
                    listview.setSelection(listview.getAdapter().getCount() - 1);
                }

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        chatAdapter.notifyDataSetChanged();
                        listview.setSelection(listview.getAdapter().getCount() - 1);
                    }
                });

            }


        class loadmessge extends AsyncTask<Void, Void, Void> {

            @Override
            protected Void doInBackground(Void... params) {
                // TODO Auto-generated method stub
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {

                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                // TODO Auto-generated method stub
                chatAdapter.notifyDataSetChanged();
                listview.onRefreshComplete();
                super.onPostExecute(result);
            }
        }

    }

这是我的listview XML:

  <com.lociiapp.PullToRefreshListView
                                android:id="@+id/listView"
                                android:layout_width="match_parent"
                                android:layout_height="150dp"
                                android:stackFromBottom="true"
                                android:transcriptMode="normal" >

我可以在Android中显示列表视图。使用chatadapter我已经应用了pull to refresh listview来加载listview中的数据1 - 1项目后刷新但它的工作正常但是我想显示刷新最后一项后进入适配器并且第一项进入顶部适配器如tat方式但是我无法做到这一点,请看我的屏幕,你会更好地了解我想做什么,请建议我将如何实现。

enter image description here

2 个答案:

答案 0 :(得分:1)

@Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            chatAdapter.notifyDataSetChanged();

            // loadPopupData(context, listView, emptyView,i);
            listview.setSelection(listview.getAdapter().getCount() - 1);
            listview.onRefreshComplete();

            super.onPostExecute(result);
        }
    }  

替换此代码并享受!!!

答案 1 :(得分:0)

在列表末尾附加聊天消息,或实施getItem以退回已撤消的项目(length - position)

查看您的代码,我认为第一个选项是最简单的:

public void addMessage(Chat chat) {
    values.add(values.size()-1, chat);
    this.notifyDataSetChanged();
}