簡體   English   中英

具有自定義光標適配器的ListView為空

[英]ListView with Custom Cursor Adapter is Empty

我正在嘗試實現高分活動,而我正在嘗試使用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);
}

}

我打電話給cursor.close(); onDestroy()方法之前。

您需要在行布局的TextView中添加android:layout_height =“ wrap_content”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM