Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在dialogbox中使用listview_Android_Listview_Dialog - Fatal编程技术网

Android 如何在dialogbox中使用listview

Android 如何在dialogbox中使用listview,android,listview,dialog,Android,Listview,Dialog,我是android新手。我正在尝试开发一个客户端应用程序。不管怎样,我有一个用户档案活动,其中有朋友和追随者,并通过textview显示计数。当用户单击关注者数量文本视图时,请将对话框上的好友列表作为列表视图查看。它创建了一个ApicClient对象,用于从服务器检索json格式的数据 friendsCountTxtView.setOnClickListener(new View.OnClickListener() { @Override public void

我是android新手。我正在尝试开发一个客户端应用程序。不管怎样,我有一个用户档案活动,其中有朋友和追随者,并通过textview显示计数。当用户单击关注者数量文本视图时,请将对话框上的好友列表作为列表视图查看。它创建了一个ApicClient对象,用于从服务器检索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) {

                }
            });
        }
    });
friendsCountTxtView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
MyTApiClients apiclients=新的MyApiClients(会话);
apiclients.getFriendsListService().show(userID,userName,-1,countForList,new Callback()){
@凌驾
公开作废成功(结果){
缓冲读取器;
StringBuilder sb=新的StringBuilder();
试一试{
reader=new BufferedReader(new InputStreamReader(result.response.getBody().in());
弦线;
而((line=reader.readLine())!=null){
某人附加(行);
}
}捕获(IOE异常){
e、 printStackTrace();
}
字符串jsonResult=sb.toString();
试一试{
JSONObject objResult=新的JSONObject(jsonResult);
nextCursor=Long.parseLong(objResult.getString(“下一个光标”);
ListViewLoaderTask ListViewLoaderTask=新建ListViewLoaderTask();
listViewLoaderTask.execute(jsonResult);
}捕获(JSONException e){
e、 printStackTrace();
}
}
@凌驾
公共失效(例外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();
       }
    }
}
私有类ListViewLoaderTask扩展异步任务{
JSONObject jObject;
/**返回用于使用Post Execute的适配器*/
@凌驾
受保护的SimpleAdapter doInBackground(字符串…strJson){
试一试{
jObject=新的JSONObject(strJson[0]);
UserProfileJsonParser UserProfileJsonParser=新的UserProfileJsonParser();
userProfileJsonParser.parse(jObject);
}捕获(例外e){
d(“JSON例外1”,例如toString());
}
UserProfileJsonParser UserProfileJsonParser=新的UserProfileJsonParser();
List userProfiles=null;
试一试{
/**将解析后的数据作为列表构造获取*/
userProfiles=userProfileJsonParser.parse(jObject);
}捕获(例外e){
Log.d(“异常”,例如toString());
}
/**Hashmap中使用的键*/
字符串[]from={“name”,“screen_name”};
/**listview\u布局中的视图ID*/
int[]to={R.id.name,R.id.screen_name};
SimpleAdapter=new SimpleAdapter(UserProfileActivity.this,userProfiles,R.layout.friends\u list\u items\u layout,from,to);
返回适配器;
}
/**由Android系统在“doInBackground”上调用,完全执行*/
/**这将在ui线程中执行*/
@凌驾
受保护的void onPostExecute(最终SimpleAdapter适配器){
dialoglist.setAdapter(适配器);
builder.setView(对话框列表);
如果(!isDialogBoxOpen)
{
isDialogBoxOpen=true;
最终对话框=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) {

        }    
    } 
dialoglist.setOnScrollListener(新的OnScrollListener(){
私有int currentVisibleItemCount;
私有状态;
私有int currentFirstVisibleItem;
私人物品;
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
//TODO自动生成的方法存根
int阈值=1;
int count=dialoglist.getCount();
如果(滚动状态==滚动状态空闲){
如果(dialoglist.getLastVisiblePosition()>=计数
-阈值){
//执行LoadMoreDataTask异步任务
新建LoadMoreDataTask().execute();
}
}
this.currentScrollState=scrollState;
这个.isScrollCompleted();
}
@凌驾
public void onScroll(AbsListView视图,int firstVisibleItem,
int visibleItemCount,int totalItemCount){
//TODO自动生成的方法存根
this.currentFirstVisibleItem=firstVisibleItem;
this.currentVisibleItemCount=visibleItemCount;
this.totalItem=totalItemCount;
}
私有无效IsCrollCompleted(){
如果(totalItem-currentFirstVisibleItem==currentVisibleItemCount
&&this.currentScrollState==SCROLL\u STATE\u IDLE){
/**在这里执行代码*/
}
}
});
}
私有类LoadMoreDataTask扩展了AsyncTask{
@凌驾
受保护的Void doInBackground(Void…参数){
MyApiClients apiclients=新的MyApiClients(会话);
apiclients.getFriendsListService().show(userID、userName、nextCursor、countForList、new Callback()){
@凌驾
公开作废成功(结果){
BufferedReader reader=null;
StringBuilder sb=新的StringBuilder();
试一试{
reader=new BufferedReader(new InputStreamReader(result.response.getBody().in());
弦线;
while((line=reader.readL)