Android ListView没有';无法通过ListView显示';s getCount()工作正常

Android ListView没有';无法通过ListView显示';s getCount()工作正常,android,listview,sqlite,Android,Listview,Sqlite,listview没有显示的问题似乎很奇怪。首先,让我发布活动中相关功能的代码: 受保护的ListView-mListView @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.result); mListView = (ListView)this.findVie

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);
        }
    });
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.result);
mListView=(ListView)this.findViewById(R.id.wrdList);
handleIntent(getIntent());
}
私人无效手册内容(意图){
if(Intent.ACTION_SEARCH.equals(Intent.getAction())){
stringword=intent.getStringExtra(SearchManager.QUERY);
//使用查询以某种方式搜索数据
//要在数据库中选择的列
字符串sel[]={“\u id”,“wort”,DictDBHelper.get2LtrLang()};
游标SrhRet=mSrhHelper.searchWord(word,sel);
//要在ListView中显示的列
字符串col[]={“wort”,DictDBHelper.get2LtrLang()};
展示列表(SrhRet、col);
SrhRet.close();
}else if(Intent.ACTION\u VIEW.equals(Intent.getAction())){
}
}
/*
*设置TextView以打印在数据库中找到的单词数
*/
受保护的无效集合计数(整数计数){
TextView tv=(TextView)this.findViewById(R.id.wrdCount);
tv.setText(String.valueOf(count));
}
/*
*设置适配器以显示列表
*第一个参数指定要在ListView中显示的行的光标
*第二个指定列
*/
受保护的无效显示列表(最终光标SrhRet,字符串[]from){
int[]to=newint[]{R.id.entry,R.id.detail};
SimpleCrsorAdapter=新的SimpleCrsorAdapter(此,
R.layout.listitem,SrhRet,from,to,
游标适配器。标志(寄存器、内容、观察者);
mListView.setAdapter(适配器);
setCount(mListView.getCount());
mListView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
//构建用于打开具有特定单词Uri的WordActivity的意图
Intent-wordIntent=newintent(getApplicationContext(),WordActivity.class);
Uri数据=Uri.withAppendedPath(DictionaryProvider.ENTRY\u CONTENT\u Uri,
字符串。valueOf(id));
setData(数据);
//更新所要求的
SrhRet.close();
mSrhHelper.updateHist(id);
startActivity(wordIntent);
}
});
}
这些函数在searchable中处理输入。我追踪了所有涉及的变量,但几乎没有发现任何异常。但是,每次我尝试此函数时,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>


提前谢谢你的帮助

我认为您的问题在于:
SrhRet.close()在您的
handleIntent(Intent-Intent)
方法中


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

常规信息,使用填充父项而不是匹配父项。match_parent现在不再使用,在某些API中会出现错误。还可以尝试将游标SrhRet设置为全局variable@Shubhayu我是在api15下发展起来的,所以我认为这一点无关紧要。我试过了。它也不起作用。有错误吗?或者你看到的只是一个空白列表?@Shubhayu无错误无警告。。如果光标被关闭(就在调用showList之后),适配器如何处理它?不确定这是否是问题所在。关闭光标旨在修改表。只要单击该项,就会调用此关闭函数。我不认为这会阻止listview的显示。或者,即使如此,为什么我的getCount()返回正确@申卡利翁索利斯特(斯里特,科罗拉多州);和SrhRet.close();。在showList(…)中调用getCount()。然后关闭光标,它仍然不工作。此函数包含在onXXListener中,而不是线性执行。但是谢谢你的建议