带有自定义光标适配器的ListView为空

时间:2012-09-20 13:44:42

标签: java android android-listview

我正在尝试实现一个高分活动,我正在尝试使用ListView。 但是有一个问题:列表视图只显示了.addHeaderView()方法添加的元素,但适配器元素似乎不可见。
尽管adapter.getCount()返回正确数量的元素,但它们在某种程度上是不可见的。

请帮忙,我把头发拉出来。

我的活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:keepScreenOn="true" >
    <ListView
        android:id="@+id/scores_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</LinearLayout>

我的ListView行布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
/>

我的活动代码:

public class ScoresActivity extends Activity   {
private ScoresAdapter adapter;
private ListView scoresList;
private Cursor cursor;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scores);

    scoresList = (ListView)findViewById(R.id.scores_layout);    

    SQLiteDatabase dataBase = ((MyApplication) getApplication()).getDataBase();     

    cursor = dataBase.query(ScoresDBHelper.SCORES_TABLE_NAME,null,null,null,null,null,null);

    adapter = new ScoresAdapter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

    scoresList.addHeaderView(getLayoutInflater().inflate(R.layout.scores_list_row, null));

    scoresList.setAdapter(adapter);

    Toast.makeText(this, String.valueOf(adapter.getCount()), Toast.LENGTH_LONG).show();
    }
}

我的适配器代码:

class ScoresAdapter extends CursorAdapter{
LayoutInflater inflater;

public ScoresAdapter(Context context, Cursor c, int flags) {
    super(context, c, flags);
    inflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView textViewName = (TextView) view.findViewById(R.id.name);
    textViewName.setText(cursor.getString(cursor.getColumnIndex("name")));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return  inflater.inflate(R.layout.scores_list_row, null);
}

}

1 个答案:

答案 0 :(得分:1)

我在打电话     cursor.close();onDestroy()方法之前。

相关问题