Friday, December 4, 2015

List on Screen : Using ListView


A List is needed for displaying playable list. The basic thinking on how to use a ListView is explained on the next paragraph. When building this example several controls are used :
  1. ListView. I place ListView in both layout xml files and in java android programs.
  2. In java android program, I use several classes :
    1. ArrayAdapter
    2. ArrayList
    3. List
    4. ListView 
On using array adapter I found out that both android.R.layout.simple_list_item_1 and android.R.id.text1 have been already defined in the library.

We need to put ListView control in layout. The snippet of xml source code is :
    /*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   
    Copyright 2015, Joko Adianto 

   */

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
/>


Afterward we need to hook java code on the ListView control named listView :
   
   /*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
    Copyright 2015, Joko Adianto
     */

public class MainActivity extends Activity {
    ListView listView;
    List<String> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        //creating empty array list of string
        ArrayList<String> arrayList = new ArrayList<String>();
        //Populate it with several string of character
        arrayList.add("AAAAA");
        arrayList.add("BAAAA");
        arrayList.add("CAAAA");
        arrayList.add("DAAAA");
        arrayList.add("EAAAA");
       //We need to have an array adapter for displaying an array list
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

        // Assign adapter to ListView
        listView.setAdapter(arrayAdapter);

         //adding another string
        arrayList.add("fAAAA");
        arrayList.add("gAAAA");
    }   

When the program running, the screen below display the list. It is a simple list view example program. It does not have any event listeners attach to it.

Picture 1. The Screen Shoot of ListView Example

On the next article about file access I will document on how to display files.

No comments:

Post a Comment