亲爱的开发者
我是新手。在 ListView 中运行时,我只会在 ListView 中显示数组的最后一个元素......我已经彻底研究了网络,并找到了一些对问题的参考,但没有一个对我的案例有帮助。
下面我将列出自定义数据库助手、布局 XML、Activity、LogCat 打印的代码。你会注意到我已经包含了一个 TextView 以及 ListView ......这只是为了帮助调试...... TextView 工作得很好。我也在运行一个 Log 指令......这个打印来记录有问题的变量。所有变量都打印正确。
.....布局文件: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>
==================================================== =======
现在这里是数据库助手的部分... 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;
}
==================================================== =====
这是活动部分 [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());
}
}
==================================================== =======================
这是 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.
==================================================== ===================
您经验丰富的指导将不胜感激。
威廉。