I worked all day to show my items what are in my database, but I can't. I've got a one table database with some items and I want to display them in ListActivity.
Here is my code:
Layout (activity_cars):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnNewCar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btnNewCar" />
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="0dip" >
</ListView>
ListActivity:
public class CarsActivity extends ListActivity {
private CarListAdapter carListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cars);
CarDbLoader carDbLoader = new CarDbLoader(getApplicationContext());
carDbLoader.open();
Cursor c = carDbLoader.fetchAll();
c.moveToFirst();
carListAdapter = new CarListAdapter(this, c);
setListAdapter(carListAdapter);
}
}
ListAdapter:
public class CarListAdapter extends CursorAdapter {
public CarListAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.car_list_row, null);
bindView(row, context, cursor);
return row;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Car car = CarDbLoader.getCarByCursor(cursor);
TextView tv = (TextView)view.findViewById(R.id.rowText);
tv.setText(car.toString());
}
}
I am sure that I've got items in the table...but I really don't know what's reason of this activity doesn't show items. Please help to find out and make the correct source.
Thanks nad Marry Chrostmas!!!!
Why you set listview's height to 0dip?
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="0dip" > // Why 0 dip? Try "fill_parent" or "match_parent"
</ListView>