MrAs
MrAs

Reputation: 77

ListView with Custom Cursor Adapter is Empty

I am trying to implement a highscores activity, and i`m trying to use a ListView. But there is a problem: list view shows me only an element what was added by .addHeaderView() method, but the adapter elements seem to be invisible.
Even though adapter.getCount() returns correct number of elements, they are somehow invisible.

Please help, I'm pulling my hair out here.

My Activity layout:

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

My ListView row layout:

<?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"
/>

My activity code:

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();
    }
}

My adapter code:

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);
}

}

Upvotes: 1

Views: 926

Answers (2)

MrAs
MrAs

Reputation: 77

I was calling cursor.close(); before onDestroy() method.

Upvotes: 1

Deepika
Deepika

Reputation: 591

You need to add android:layout_height ="wrap_content" in your row layout's TextView.

Upvotes: 0

Related Questions