ListView: is a View which groups several items and display them in a vertical scrollable list.
Adapter: Adapter is bridge between UI component and the data which fills that UI component.
Out put will look like below:
Here is the source code: listview
1. Create Project with name ListView with Activity name as MainActivity.java.
2. Open activity_main.xml and paste below code(for listview)
2. Open strings.xml and paste below code.
2. create list_item inside res ->values
paste below code
3. Open MainActivity.java and paste below code
Adapter: Adapter is bridge between UI component and the data which fills that UI component.
Out put will look like below:
Here is the source code: listview
1. Create Project with name ListView with Activity name as MainActivity.java.
2. Open activity_main.xml and paste below code(for listview)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.listview.MainActivity"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview" > </ListView> </RelativeLayout>
2. Open strings.xml and paste below code.
<string-array name="friends">
<item>Friend A</item>
<item>Friend B</item>
<item>Friend C</item>
<item>Friend D</item>
<item>Friend E</item>
<item>Friend F</item>
<item>Friend G</item>
<item>Friend H</item>
<item>Friend I</item>
<item>Friend J</item>
<item>Friend K</item>
<item>Friend L</item>
<item>Friend M</item>
<item>Friend N</item>
<item>Friend O</item>
<item>Friend P</item>
</string-array>
2. create list_item inside res ->values
paste below code
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/friend_name" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:padding="10dp" android:text="asdf" android:layout_centerHorizontal="true" android:textSize="20dp" android:textAllCaps="true" android:textStyle="bold" > </TextView>
3. Open MainActivity.java and paste below code
package com.listview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //creating object of listview listView = (ListView)findViewById(R.id.listview); //getting string array from resourses(values->strings.xml->friends) final String[] friends = getResources().getStringArray(R.array.friends); //creating array adapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item,friends); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //showing toast message Toast.makeText(getApplicationContext(),friends[position],Toast.LENGTH_LONG).show(); } }); } }
No comments:
Post a Comment