2012-04-12 22 views
0

這個問題,listview沒有顯示出來似乎很奇怪。首先讓我發佈我的活動代碼: protected ListView mListView;雖然ListView的getCount()可以正常工作,但ListView並未顯示

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.result);  
    mListView = (ListView)this.findViewById(R.id.wrdList); 

    handleIntent(getIntent()); 
} 

private void handleIntent(Intent intent) { 
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String word = intent.getStringExtra(SearchManager.QUERY); 
     //use the query to search your data somehow 

     //Columns to be selected in database 
     String sel[] = {"_id", "wort", DictDBHelper.get2LtrLang()}; 
     Cursor SrhRet = mSrhHelper.searchWord(word, sel); 
     //Columns to be showed in ListView 
     String col[] = {"wort", DictDBHelper.get2LtrLang()}; 
     showList(SrhRet, col); 
     SrhRet.close(); 
    } else if (Intent.ACTION_VIEW.equals(intent.getAction())) { 

    } 
} 

/* 
* Set the TextView to print how many words are found in database 
*/ 
protected void setCount(int count) { 
    TextView tv = (TextView)this.findViewById(R.id.wrdCount); 
    tv.setText(String.valueOf(count)); 
} 

/* 
* Set the adapter to show the list 
* First parameter specifies the cursor of rows to be shown in ListView 
* Second one specifies columns 
*/ 
protected void showList(final Cursor SrhRet, String[] from) { 

    int[] to = new int[]{R.id.entry, R.id.detail}; 

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.listitem, SrhRet, from, to, 
      CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); 
    mListView.setAdapter(adapter); 
    setCount(mListView.getCount()); 

    mListView.setOnItemClickListener(new OnItemClickListener(){   
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // Build the Intent used to open WordActivity with a specific word Uri 
      Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class); 
      Uri data = Uri.withAppendedPath(DictionaryProvider.ENTRY_CONTENT_URI, 
              String.valueOf(id)); 
      wordIntent.setData(data); 
      //Required by the update 
      SrhRet.close(); 
      mSrhHelper.updateHist(id); 
      startActivity(wordIntent); 
     } 
    }); 
} 

這些函數處理可搜索的輸入。我追蹤了所有涉及的變量,但幾乎找不到任何異常。然而每次我嘗試這個函數時,TextView都會顯示ListView應該包含的正確數字!這些函數在之前完美工作,而且我從另一個派生出來的另一個活動調用這些功能也很好。

我用listitem.xml在ListView的項目內容:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<TextView 
    android:id="@+id/entry" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:textSize="17dp" /> 
<TextView 
    android:id="@+id/detail" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 

而且我爲result.xml。我已經仔細檢查了方向。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 
<TextView 
    android:id="@+id/wrdCount" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 
<ListView 
    android:id="@+id/wrdList" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" /> 
</LinearLayout> 

感謝您的幫助!

+0

一般信息,請使用fill_parent而不是match_parent。 match_parent現在不再使用,並會在某些API中給出錯誤。也嘗試使光標SrhRet全局變量 – Shubhayu 2012-04-12 10:46:58

+0

@Shubhayu我開發下api15所以我不認爲這個問題。我試過了。它也不起作用。 – Hypeboyz 2012-04-12 10:53:36

+0

是否有任何錯誤或您是否看到空白列表? – Shubhayu 2012-04-12 11:06:58

回答

0

我認爲你的問題是這樣的:SrhRet.close();在你的handleIntent(Intent intent)方法。

請嘗試關閉活動的onDestroy()方法中的遊標。

+0

關閉遊標旨在修改表格。只要我點擊物品,就會調用這個關閉功能。我不認爲這個阻止顯示列表視圖。或者,即使這樣做,爲什麼我的getCount()返回正確? @Deucalion – Hypeboyz 2012-04-12 14:14:26

+0

showList(SrhRet,col);和SrhRet.close();.你可以在showList(...)中調用getCount()。之後關閉光標。 – Blehi 2012-04-12 16:10:44

+0

它仍然不起作用。該函數包含在onXXListener中,而不是線性執行。但謝謝你的建議 – Hypeboyz 2012-04-13 00:27:26

相關問題