2013-02-20 40 views
0

我有我的Android應用程序,它顯示的數據從下載&解析一個JSON的列表視圖。在這個列表視圖中,OnItemClickListener在第一個五或六個項目後沒有響應。可以請任何人告訴我什麼是我的問題?列表查看第一個項目後沒有迴應OnItemClickListener

這裏是我的列表視圖代碼:

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就是JSON字符串在它下載一個的AsyncTask是doInBackGround方法&它打開另一個的AsyncTask在它的onPostExecute方法。 在第二個asyncTask中,我解析了doInBackground方法中的json &使用onPostExecute方法中的適配器加載列表視圖。 二的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; 
    } 

回答

4

好像你的觀點並沒有考慮重點listview.It的角度問題,當您單擊列表行,它總是捕捉其他視圖事件。將下面的屬性應用於行文件中的主佈局,並讓我知道它的作品與否?

android:descendantFocusability="blocksDescendants"

0

羅賓漢,感謝您的回答,它可以幫助我。

在我的情況下,我使用ButtonCursorAdapter的自定義視圖。如果按鈕是不可見的或者不見了 - 一切正常,但是如果我將它設置爲可見,那麼ListView上的setOnItemClickListener不起作用。

+0

Robinhoods的答案也爲我解決了它。 – Theo 2013-11-08 10:58:02

相關問題