1

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.

1
  • Set list adapter after creating list array. in above code you are create new adapter every time so in list there is only set last adapter value that's way in your list only last value added.Try to make this change than it's work perfectly. Commented Nov 5, 2012 at 11:22

1 Answer 1

1

You create a new adapter, and assign it to the list for each element.

  1. Create your array first.
  2. Assign this array to your adapter
  3. Assign this adapter to your list

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);
Sign up to request clarification or add additional context in comments.

2 Comments

THX for feedback. I have now tried applying above. My variables : values and vehicleList now cannot be resolved [needs initialization?]
Sorry I forgot something. Try to look at that. i havent tested it, i cannot do it for now, but it should be the way to do it. You should try to look at how use an adapter taking an array list in parameter ;) It will be a lot easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.