Reputation: 21
Dear Fellow Developers
I'm a newby. When running in ListView I am only getting the last element of the array to display within the ListView ... I have thoroughly researched the net and have found some reference to the problem but none that have helped my case.
Below I shall list the code for a custom database helper, the layout XML, the Activity, LogCat print. You will note that I have included a TextView as well as the ListView ... this is only to assist with debugging ... the TextView works perfectly. I am also running a Log directive ... this to print to log the variables in question. All the variables are printing correctly.
..... Layout File : activity_vehicles_fp.xml .......
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableVehicleAdd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableRow
android:id="@+id/rowTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/lblTitle"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_VehicleFP"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="30dp"
android:gravity="center"/>
</TableRow>
<TableRow
android:id="@+id/rowListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="@android:id/list"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</TableRow>
<TableRow
android:id="@+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/listTV"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</TableRow>
</TableLayout>
=========================================================
Now here is the section of the Database helper ... EngenDataClass.java :
public List<Vehicles> getAllVehicles() {
List<Vehicles> vehicleList = new ArrayList<Vehicles>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_VEHICLES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Vehicles vehicle = new Vehicles();
vehicle.setRegNumber(cursor.getString(1));
vehicle.setMake(cursor.getString(2));
// Adding vehicle to list
vehicleList.add(vehicle);
} while (cursor.moveToNext());
}
// return Vehicle List
return vehicleList;
}
=======================================================
Here is the section of activity [VehiclesActivityFP.java] :
public class VehiclesActivityFP extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vehicles_fp);
final EngenDataClass db = new EngenDataClass(this);
ListView listView = getListView();
List<Vehicles> vehicles = db.getAllVehicles();
// Listing all Vehicles via TextView
Log.d("Reading: ", "Reading all Vehicles ..");
for (Vehicles allVeh : vehicles) {
String vehicleList = (allVeh.getMake() + "\t" + allVeh.getRegNumber() + "\n");
String[] values = new String []{ (vehicleList )};
// Assign adapter to ListView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,(values));
listView.setAdapter(adapter);
//Print to TextView
TextView myTextView = (TextView) findViewById(R.id.listTV);
myTextView.append(vehicleList.toString());
//Writing Vehicles to log
Log.d("vehicleList: ", vehicleList.toString());
Log.d ("values : ", values.toString());
Log.d ("values : ", vehicles.toString());
}
}
========================================================================
Here is the LogCat :
11-05 10:38:10.267: D/Reading:(866): Reading all Vehicles ..
11-05 10:38:10.267: D/vehicleList:(866): TEST TEST
11-05 10:38:10.267: D/values :(866): [Ljava.lang.String;@41214360
11-05 10:38:10.267: D/values :(866): [com.BIM.engen.log.Vehicles@41213fc8, com.BIM.engen.log.Vehicles@412140c0, com.BIM.engen.log.Vehicles@41214170, com.BIM.engen.log.Vehicles@41214220]
11-05 10:38:10.277: D/vehicleList:(866): TEST TEST
11-05 10:38:10.277: D/values :(866): [Ljava.lang.String;@412152c0
11-05 10:38:10.277: D/values :(866): [com.BIM.engen.log.Vehicles@41213fc8, com.BIM.engen.log.Vehicles@412140c0, com.BIM.engen.log.Vehicles@41214170, com.BIM.engen.log.Vehicles@41214220]
11-05 10:38:10.277: D/vehicleList:(866): TESTRR TESTTT
11-05 10:38:10.277: D/values :(866): [Ljava.lang.String;@41215ed0
11-05 10:38:10.277: D/values :(866): [com.BIM.engen.log.Vehicles@41213fc8, com.BIM.engen.log.Vehicles@412140c0, com.BIM.engen.log.Vehicles@41214170, com.BIM.engen.log.Vehicles@41214220]
11-05 10:38:10.287: D/vehicleList:(866): SE PAULA
11-05 10:38:10.287: D/values :(866): [Ljava.lang.String;@41216b60
11-05 10:38:10.287: D/values :(866): [com.BIM.engen.log.Vehicles@41213fc8, com.BIM.engen.log.Vehicles@412140c0, com.BIM.engen.log.Vehicles@41214170, com.BIM.engen.log.Vehicles@41214220]
11-05 10:38:10.487: I/Choreographer(866): Skipped 43 frames! The application may be doing too much work on its main thread.
11-05 10:38:10.827: I/Choreographer(866): Skipped 61 frames! The application may be doing too much work on its main thread.
====================================================================
Your experienced guidance will be most appreciated.
William.
Upvotes: 1
Views: 1803
Reputation: 4855
You create a new adapter, and assign it to the list for each element.
So :
List<String> list = new ArrayList<String>();
for (Vehicles allVeh : vehicles) {
String vehicle = (allVeh.getMake() + "\t" + allVeh.getRegNumber() + "\n");
list.add(vehicle)
}
String [] values = list.toArray(new String[list.size()]);
// Assign adapter to ListView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,(values));
listView.setAdapter(adapter);
Upvotes: 1