ListView在前几项之后没有响应OnItemClickListener[英] Listview doesn't respond to OnItemClickListener after first few items

本文是小编为大家收集整理的关于ListView在前几项之后没有响应OnItemClickListener的处理方法,想解了ListView在前几项之后没有响应OnItemClickListener的问题怎么解决?ListView在前几项之后没有响应OnItemClickListener问题的解决办法?ListView在前几项之后没有响应OnItemClickListener问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我在我的Android应用程序中有一个ListView,它显示从下载和解析JSON的数据.在这个listview中,在前五个或六个项目之后没有响应OnItemClickListener.Can请告诉我我的问题是什么?

这是我的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是一个asynctask,其中Json String在它的DoInbackground方法中下载,它是在它的onPostexecute方法中打开另一个ASYNCTASK. 在第二个ASYNCTASK中,我在DoInbackground方法中解析了JSON,并在onPostExecute方法中加载ListView. 第二个ASYNCTASK的代码:

    /** 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;
    }

推荐答案

似乎您的视图未在ListView.it中拍摄焦点.单击列表行时,视图问题是捕获的其他视图事件.将以下属性应用于行文件中的主要布局,并告诉我其有效吗?

android:descendantFocusability="blocksDescendants"

其他推荐答案

robinhood,谢谢你的答案,它可以帮助我.

在我的情况下,我使用了带有按钮的ContactScursorAdapter的自定义视图.如果按钮是不可见的或消失 - 一切都可以正常工作,但如果我将其设置为可见,则ListView上的SetOnitemclickListener不起作用.

本文地址:https://www.itbaoku.cn/post/161057.html

问题描述

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"

其他推荐答案

RobinHood, thanks for the answer, it helps me.

In my case, I used custom view for ContactsCursorAdapter with Button. If Button is Invisible or Gone - everything works fine, but if i set it as Visible, then setOnItemClickListener on the ListView didn't work.

查看更多