List of scrollable items can be displayed in Android
using ListView It helps you to displaying the data in the form of a
scrollable list. Users can then select any list item by clicking on it. ListView is default scrollable so we do not need to use scroll View or
anything else with ListView.
To fill the data in a ListView we simply use adapters.
List items are automatically inserted to a list using an Adapter that pulls the content from a source such as an
arraylist, array or database.
Attibute of Adapter:-
1. id: id
is used to uniquely identify a ListView
2.
divider: This is a drawable or color to draw
between different list items.
3.
dividerHeight: This specify the height of the
divider between list items.
4. listSelector: listSelector
property is used to set the selector of the listView.<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/material_blue_grey_800"
android:dividerHeight="1dp" />
</LinearLayout>
Uses Of Adapter:-
An Adapter is a bridge
between UI component and data source It holds the data and send the data
to adapter view then view can
takes the data from the adapter view and shows the data on different views like
as list view, grid view, spinner etc.
An
android commonly used adapters are:
1.Array Adapter
2.Base Adapter
1 Array Adapter:Whenever
you have a list of single items which is backed by an array, you can use
ArrayAdapter. For example, list of phone contacts, list of vegitable name and
so on.
Example of list view using Array
Adapter:
Step 1: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/material_blue_grey_800"
android:dividerHeight="1dp" />
</LinearLayout>
Step 2: activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_marginTop="20dp"
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="25dp"
android:layout_gravity="center"/>
</LinearLayout>
Step 3: MainActivity.java
package com.example.CodeWithAndroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;import android.widget.ListView;
public class MainActivity extends Activity
{
// Array of strings...
ListView simpleList;
String countryList[] = {"Java", "Php", ".net", "C language", "C++", "Kotlin"};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.activity_listview, R.id.textView, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
2.Base Adapter:BaseAdapter is a common base class
of a general implementation of an Adapter that can be used in ListView.
Whenever you need a customized list you create your own adapter and extend base
adapter in that. Base Adapter can be extended to create a custom Adapter for
displaying a custom list item. ArrayAdapter is also an implementation of
BaseAdapter.
Example of list view
using Custom adapter(Base adapter):
Step 1: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/material_blue_grey_800"
android:dividerHeight="1dp" />
</LinearLayout>
Step 2: activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_marginTop="20dp"
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="25dp"
android:textColor="#F00737" />
</LinearLayout>
Step 3: MainActivity.java
package com.example.Codewithandroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView simpleList;
String countryList[] = {"java", ".net", "php", "google", "yahooo", "clanguage"};
int flags[] = {R.drawable.java, R.drawable.net, R.drawable.php, R.drawable.google, R.drawable.yahoo, R.drawable.c_langugae};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), countryList, flags);
simpleList.setAdapter(customAdapter);
}
}
Step 4: CustomAdapter.java
package com.example.Codewithandroid;
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.zip.Inflater;
public class CustomAdapter extends BaseAdapter {
Context context;
String countryList[];
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, String[] countryList, int[] flags) {
this.context = context;
this.countryList = countryList;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return countryList.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.activity_listview, null);
TextView country = (TextView) view.findViewById(R.id.textView);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
country.setText(countryList[i]);
icon.setImageResource(flags[i]);
return view;
}
}
1 Comments
Although these gamers probably to|are inclined to} consume the area bar of their keyboard and the left button of their mouse much quicker than another participant on earth. Developers paid a reasonably penny to lock in the rights to bring 카지노사이트 to the market slots that feature well-liked manufacturers and franchises. You do not must be a psychologist, a consumed marketer, or a casino operator to understand that.
ReplyDelete