2012-04-12 11 views
0

リストビューが表示されないというこの問題は非常に奇妙なようです。まず、アクティビティに関連する機能のコードを投稿させてください。 保護されたリストビューmListView;ListViewのgetCount()は正しく動作しますが、リストビューは表示されません

@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に含める正しい数字が表示されます。これらの関数は以前より完全に機能していました。この関数から派生した別のアクティビティもうまく機能するこれらの関数を呼び出します。

私は、ListViewコントロールの項目内のコンテンツとしてlistitem.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/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

一般情報は、match_parentの代わりにfill_parentを使用してください。 match_parentは現在使用されておらず、一部のAPIではuエラーが発生します。また、Cursor SrhRetをグローバル変数にしてみてください。 – Shubhayu

+0

@Shubhayu私はapi15の下で開発したので、これは問題ではないと思います。そして私はそれを試みました。どちらもうまくいきません。 – Hypeboyz

+0

エラーがある場合、または空のリストが表示されている場合は表示されます。 – Shubhayu

答えて

0

handleIntent(Intent intent)の方法であなたの問題はSrhRet.close();と思われます。

アクティビティのonDestroy()メソッドでカーソルを閉じるようにしてください。

+0

カーソルを閉じるには、テーブルを変更することを意図しています。このクローズ機能は、アイテムをクリックしたときに呼び出されます。私はこれがリストビューが表示されるのを妨げるとは思わない。それとも、私のgetCount()が正しく戻ってくるのですか? @Deucalion – Hypeboyz

+0

showList(SrhRet、col);とSrhRet.close();があります。 showList(...)でgetCount()を呼び出します。その後、カーソルを閉じます。 – Blehi

+0

それでも動作しません。この関数は、線形に実行されるのではなく、onXXListenerで囲まれています。しかし、あなたのアドバイスに感謝 – Hypeboyz

関連する問題