Tuesday, March 16, 2021

Android ListView

 


Android ListView

Android ListView is a view which contains the group of items and displays in a scrollable list. ListView is implemented by importing android.widget.ListView class. ListView is a default scrollable which does not use other scroll view.

ListView uses Adapter classes which add the content from data source (such as string array, array, database etc) to ListView. Adapter bridges data between an AdapterViews and other Views (ListView, ScrollView etc).

Example of ListView

Let's implement a simple listview example.


Structure of listview project

android Listview 1

activity_main.xml

First we need to drag and drop ListView component from palette to activity_main.xml file.

File: activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="listview.example.com.listview.MainActivity">  
  8.   
  9.     <ListView  
  10.         android:id="@+id/listView"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="fill_parent"  
  13.          />  
  14. </android.support.constraint.ConstraintLayout>  

Create an additional mylist.xml file in layout folder which contains view components displayed in listview.

mylist.xml

File: mylist.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:id="@+id/textView"  
  5.     android:layout_width="wrap_content"  
  6.     android:layout_height="wrap_content"  
  7.     android:text="Medium Text"  
  8.     android:textStyle="bold"  
  9.     android:textAppearance="?android:attr/textAppearanceMedium"  
  10.     android:layout_marginLeft="10dp"  
  11.     android:layout_marginTop="5dp"  
  12.     android:padding="2dp"  
  13.     android:textColor="#4d4d4d"  
  14.      />  

Now place the list of data in strings.xml file by creating string-array.

strings.xml

File:strings.xml

  1. <resources>  
  2.     <string name="app_name">ListView</string>  
  3.     <string-array name="array_technology">  
  4.         <item>Android</item>  
  5.         <item>Java</item>  
  6.         <item>Php</item>  
  7.         <item>Hadoop</item>  
  8.         <item>Sap</item>  
  9.         <item>Python</item>  
  10.         <item>Ajax</item>  
  11.         <item>C++</item>  
  12.         <item>Ruby</item>  
  13.         <item>Rails</item>  
  14.         <item>.Net</item>  
  15.         <item>Perl</item>  
  16.     </string-array>  
  17. </resources>  

Activity class

In java class we need to add adapter to listview using setAdapter() method of listview.

File: MainActivity.java

  1. package listview.example.com.listview;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.AdapterView;  
  7. import android.widget.ArrayAdapter;  
  8. import android.widget.ListView;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends AppCompatActivity {  
  13.     ListView listView;  
  14.     TextView textView;  
  15.     String[] listItem;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         listView=(ListView)findViewById(R.id.listView);  
  22.         textView=(TextView)findViewById(R.id.textView);  
  23.         listItem = getResources().getStringArray(R.array.array_technology);  
  24.         final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  
  25.                 android.R.layout.simple_list_item_1, android.R.id.text1, listItem);  
  26.         listView.setAdapter(adapter);  
  27.   
  28.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  29.             @Override  
  30.             public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {  
  31.                 // TODO Auto-generated method stub  
  32.                 String value=adapter.getItem(position);  
  33.                 Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();  
  34.   
  35.             }  
  36.         });  
  37.     }  
  38. }  

Output

android Listview 2
android Listview 3 



No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile