亲爱的开发人员
我是个新人。 在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.
=============================================== =====================
非常感谢您经验丰富的指导。
威廉。
答案 0 :(得分:1)
您创建一个新适配器,并将其分配给每个元素的列表。
所以:
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);