如何在对话框中使用listview

时间:2015-09-17 13:43:06

标签: android listview dialog

我是android的新人。我正在尝试开发一个客户端app.Anyway,我有用户个人资料活动,其中有朋友和关注者,并且正在使用textview显示计数。当用户点击关注者计数时,textview会在对话框中看到朋友列表作为列表视图。它创建了一个apiclient对象,用于从服务器检索数据为json格式。

friendsCountTxtView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            MyTApiClients apiclients = new MyApiClients(session);

            apiclients.getFriendsListService().show(userID, userName, -1, countForList, new Callback<Response>() {
                @Override
                public void success(Result<Response> result) {

                    BufferedReader reader;
                    StringBuilder sb = new StringBuilder();

                    try {
                        reader = new BufferedReader(new InputStreamReader(result.response.getBody().in()));
                        String line;

                        while ((line = reader.readLine()) != null) {
                            sb.append(line);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    String jsonResult = sb.toString();

                    try {
                        JSONObject objResult = new JSONObject(jsonResult);
                        nextCursor = Long.parseLong(objResult.getString("next_cursor"));

                        ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
                        listViewLoaderTask.execute(jsonResult);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void failure(Exception e) {

                }
            });
        }
    });

我使用asyncTask解析jsonobject并将其绑定到listview。

private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter> {

    JSONObject jObject;

    /** Returning an Adapter For Using Post Execute*/
    @Override
    protected SimpleAdapter doInBackground(String... strJson) {
        try{

            jObject = new JSONObject(strJson[0]);
            UserProfileJsonParser userProfileJsonParser = new UserProfileJsonParser();
            userProfileJsonParser.parse(jObject);

        }catch(Exception e){

            Log.d("JSON Exception1", e.toString());

        }

        UserProfileJsonParser userProfileJsonParser = new UserProfileJsonParser();

        List<HashMap<String, String>> userProfiles = null;

        try{
            /** Getting the parsed data as a List construct */
            userProfiles = userProfileJsonParser.parse(jObject);
        }catch(Exception e){
            Log.d("Exception",e.toString());
        }

        /** Keys used in Hashmap */
        String[] from = { "name","screen_name"};

        /** Ids of views in listview_layout */
        int[] to = { R.id.name,R.id.screen_name};


        SimpleAdapter adapter = new SimpleAdapter(UserProfileActivity.this, userProfiles, R.layout.friends_list_items_layout, from, to);

        return adapter;
    }

    /** Invoked by the Android system on "doInBackground" is executed completely */
    /** This will be executed in ui thread */
    @Override
    protected void onPostExecute(final SimpleAdapter adapter) {

        dialoglist.setAdapter(adapter);
        builder.setView(dialoglist);

        if (!isDialogBoxOpen)
       {
           isDialogBoxOpen = true;
           final Dialog dialog = builder.create();
           dialog.show();
       }
    }
}

稍后当用户滚动listview时,它会执行另一个异步方法来加载更多数据。

 dialoglist.setOnScrollListener(new OnScrollListener() {
        private int currentVisibleItemCount;
        private int currentScrollState;
        private int currentFirstVisibleItem;
        private int totalItem;

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

            int threshold = 1;
            int count = dialoglist.getCount();

            if (scrollState == SCROLL_STATE_IDLE) {
                if (dialoglist.getLastVisiblePosition() >= count
                        - threshold) {
                    // Execute LoadMoreDataTask AsyncTask
                    new LoadMoreDataTask().execute();
                }
            }

            this.currentScrollState = scrollState;
            this.isScrollCompleted();
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            this.currentFirstVisibleItem = firstVisibleItem;
            this.currentVisibleItemCount = visibleItemCount;
            this.totalItem = totalItemCount;

        }

        private void isScrollCompleted() {
            if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                    && this.currentScrollState == SCROLL_STATE_IDLE) {
                /** To do code here*/
            }
        }
    });
}
    private class LoadMoreDataTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            MyApiClients apiclients = new MyApiClients(session);

            apiclients.getFriendsListService().show(userID, userName, nextCursor, countForList, new Callback<Response>() {
                @Override
                public void success(Result<Response> result) {

                    BufferedReader reader = null;
                    StringBuilder sb = new StringBuilder();

                    try {
                        reader = new BufferedReader(new InputStreamReader(result.response.getBody().in()));
                        String line;

                        while ((line = reader.readLine()) != null) {
                            sb.append(line);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    String jsonResult = sb.toString();

                    try {
                        JSONObject objResult = new JSONObject(jsonResult);
                        nextCursor = Long.parseLong (objResult.getString("next_cursor"));
                        ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
                        listViewLoaderTask.execute(jsonResult);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void failure(Exception e) {

                }
            });

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

        }    
    } 

最后,当我们加载更多数据时,我再次调用listViewLoaderTask来绑定来自服务的检索数据。

它有效但有一些问题。当提醒数据到来时(5行)对话框变小。有时当我点击文本视图时,它不会打开对话框。

问题1:如何使我的对话框稳定。是自定义对话效率解决方案。

问题2:我使用异步方法进行此操作。你认为我的使用对吗?你为我的这项任务提供了哪些最佳实践方法?

0 个答案:

没有答案