列表视图后不前几个项目OnItemClickListener回应 [英] Listview doesn't respond to OnItemClickListener after first few items

查看:110
本文介绍了列表视图后不前几个项目OnItemClickListener回应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序是显示从下载&放数据列表视图;解析JSON。在此列表视图,OnItemClickListener不是第一五六响应后items.Can有人告诉我什么是我的问题?

下面是我的code为列表视图:

  mListView.setOnItemClickListener(新OnItemClickListener(){        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                长ARG3){
            // TODO自动生成方法存根
            尝试{
                字符串s = BookJsonParser.ids [ARG2]
                字符串bookDetailUrl = URL + S;
                DownloadBookDetail downloadTaskbookDetail =新DownloadBookDetail();
                downloadTaskbookDetail.execute(bookDetailUrl);            }赶上(例外五){
                的System.out.println(e.printStackTrace(););
            }
        }    });

DownloadBookDetail就是JSON字符串在它下载一个的AsyncTask的doInBackground方法和放大器;它打开另一个的AsyncTask在它的onPostExecute方法。
在第二AsyncTask的,我解析在doInBackground方法和放JSON;装载在onPostExecute方法的适配器列表视图。
其次的AsyncTask的code:

  / **的AsyncTask来解析JSON数据和负载的ListView * /
私有类ListViewLoaderTask扩展
        AsyncTask的<弦乐,太虚,SimpleAdapter> {    的JSONObject jObject;    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        pDialog =新ProgressDialog(BookActivity.this);
        pDialog.setMessage(「上市新书......);
        pDialog.setIndeterminate(假);
        pDialog.setCancelable(假);
        pDialog.show();
    }    //进行XML数据的解析中的非UI线程
    @覆盖
    保护SimpleAdapter doInBackground(字符串... strJson){
        尝试{
            jObject =新的JSONObject(strJson [0]);
            BookJsonParser countryJsonParser =新BookJsonParser();
            countryJsonParser.parse(jObject);
        }赶上(例外五){
            Log.d(JSON Exception1,e.toString());
        }        //实例化JSON解析器类
        BookJsonParser countryJsonParser =新BookJsonParser();        //一个列表对象来存储解析国家名单
        清单<&HashMap的LT;弦乐,对象>>国家= NULL;        尝试{
            //获取分析数据作为一个列表构造
            国家= countryJsonParser.parse(jObject);
            的System.out.println(countries.toString());
        }赶上(例外五){
            Log.d(异常,e.toString());
        }        //在哈希映射使用的键
        的String [] =从{国家,国旗,作者};        的意见listview_layout // IDS
        INT []为= {R.id.tv_bookName,R.id.list_image,R.id.tv_bookAuthor};        // /////////
        / *
         *的for(int i = 0; I< BookJsonParser.ids.length;我++){
         *的System.out.println(BookJsonParser.ids [I]); }
         * /        //实例化一个适配器来存储每个项目
        // R.layout.listview_layout定义每个项目的布局
        SimpleAdapter适配器=新SimpleAdapter(getBaseContext()
                国家R.layout.item_lv_layout,从,到);        返回适配器;
    }


解决方案

好像你的观点不采取重点listview.It的角度问题,当您单击列表行,它总是捕捉其他视图的事件。下面申请财产主布局行的文件,并让我知道它的工作或没有?

的android:descendantFocusability =blocksDescendants

I have a listview in my android app which is showing data from downloading & parsing a json. In this listview, OnItemClickListener is not responding after first five or six items.Can anybody please tell me what is my problem ?

Here is my code for listview :

mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            try {                   
                String s = BookJsonParser.ids[arg2];
                String bookDetailUrl =  url + s;
                DownloadBookDetail downloadTaskbookDetail = new DownloadBookDetail();
                downloadTaskbookDetail.execute(bookDetailUrl);

            } catch (Exception e) {
                System.out.println(e.printStackTrace(););
            }
        }

    });

DownloadBookDetail is an asyncTask where Json String is downloading in it's doInBackGround method & it is opening another asyncTask in it's onPostExecute method. In 2nd asyncTask, I am parsing json in doInBackground method & loading the listview with a adapter in onPostExecute method. Second asyncTask's code :

    /** AsyncTask to parse json data and load ListView */
private class ListViewLoaderTask extends
        AsyncTask<String, Void, SimpleAdapter> {

    JSONObject jObject;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(BookActivity.this);
        pDialog.setMessage("Listing New Books...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    // Doing the parsing of xml data in a non-ui thread
    @Override
    protected SimpleAdapter doInBackground(String... strJson) {
        try {
            jObject = new JSONObject(strJson[0]);
            BookJsonParser countryJsonParser = new BookJsonParser();
            countryJsonParser.parse(jObject);
        } catch (Exception e) {
            Log.d("JSON Exception1", e.toString());
        }

        // Instantiating json parser class
        BookJsonParser countryJsonParser = new BookJsonParser();

        // A list object to store the parsed countries list
        List<HashMap<String, Object>> countries = null;

        try {
            // Getting the parsed data as a List construct
            countries = countryJsonParser.parse(jObject);
            System.out.println(countries.toString());
        } catch (Exception e) {
            Log.d("Exception", e.toString());
        }

        // Keys used in Hashmap
        String[] from = { "country", "flag", "author" };

        // Ids of views in listview_layout
        int[] to = { R.id.tv_bookName, R.id.list_image, R.id.tv_bookAuthor };

        // /////////
        /*
         * for (int i = 0; i < BookJsonParser.ids.length; i++) {
         * System.out.println(BookJsonParser.ids[i]); }
         */

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                countries, R.layout.item_lv_layout, from, to);

        return adapter;
    }

解决方案

Seem your view is not taking focus in listview.It's the issue of view when you click the list row, it is always the capture other view event. apply below property to your main layout in row file and let me know whether its works or not?

android:descendantFocusability="blocksDescendants"

这篇关于列表视图后不前几个项目OnItemClickListener回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆