ListView不通过ListView getCount()正常工作

时间:2012-04-12 10:39:40

标签: android listview sqlite

listview没有显示的这个问题似乎非常奇怪。首先让我发布我在活动中涉及的功能代码:         受保护的ListView mListView;

@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>

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我认为您的问题就是:SrhRet.close();方法中的handleIntent(Intent intent)

请尝试在活动的onDestroy()方法中关闭光标。