在前几个项目之后,Listview不响应OnItemClickListener

时间:2013-02-20 04:27:54

标签: android listview onitemclicklistener

我的Android应用程序中有一个列表视图,显示来自下载和数据的数据。解析一个json。在这个列表视图中,OnItemClickListener在前五个或六个项目后没有响应。任何人都可以告诉我我的问题是什么?

这是我的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方法&amp;它在onPostExecute方法中打开另一个asyncTask。 在第二个asyncTask中,我在doInBackground方法&amp;中解析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;
    }

2 个答案:

答案 0 :(得分:4)

似乎你的观点没有把重点放在listview上。当你单击列表行时,它是查看的问题,它始终是捕获其他视图事件。将以下属性应用于行文件中的主要布局,并告诉我它是否有效?

android:descendantFocusability="blocksDescendants"

答案 1 :(得分:0)

罗宾汉,谢谢你的回答,这对我很有帮助。

就我而言,我使用Button的ContactsCursorAdapter自定义视图。如果Button是Invisible或Gone - 一切正常,但如果我将其设置为Visible,则ListView上的setOnItemClickListener不起作用。