1

親愛なる開発者の皆様

私は初心者です。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.

================================================== ==================

経験豊富なご指導をよろしくお願いいたします。

ウィリアム。

4

1 に答える 1

1

新しいアダプタを作成し、それを各要素のリストに割り当てます。

  1. 最初にアレイを作成します。
  2. このアレイをアダプタに割り当てます
  3. このアダプタをリストに割り当てます

それで :

     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);
于 2012-11-05T08:57:16.140 に答える