ListView dan Spinner pada Android Studio
Wednesday, 26 September 2018
Add Comment
Hai kembali lagi ke blog saya malasnugas.com . Disini saya akan menjelaskan sedikit tentang penggunaan widget list view dan spinner pada android studio . simak baik-baik ya ...
ListView adalah view group android yang digunakan untuk menampilkan item dengan tampilan secara vertical list. ListView memiliki properti wajib yaitu layout_height, layout_width, dan ID. ListView juga memiliki class ListActivity yang khusus menangani ListView.
ListActivity
ListActivity adalah class Activity yang di khususkan untuk ListView. Sama seperti class activity, ListActivity juga memiliki lifecycle. Yang membedakan dari kedua class ini tarletan pada penggunaan ListActivity yang lebih sepesifik untuk ListView ketimbang Activity yang memiliki lingkup global.
Arra yAdapter
Setiap ListView memiliki adapter, yang berfungsi sebagai array item dari ListView. ArrayAdapter adalah kumpulan item-item list yang nantinya akan digunakan oleh listview untuk ditampilkan.
C ustom Adapter
Setiap item dapat di custom sesuai keinginan kita. Hal ini dilakukan untuk membuat listview menjadi lebih menarik dan interaktif. Di satu sisi kita juga ingin menampilkan informasi lebih banyak di setiap item listview. Perhatikan contoh dibawah ini :
Gambar diatas adalah halaman pemberitahuan yang ada pada aplikasi
android. Halaman notifikasi tersebut merupakan listview yang
adapter-nya di custom sedemikian rupa sehingga menampikan informasi
yang di perlukan. Spinner serupa dengan ListView.
Agar kita lebih mengerti tentang penggunaan widget list view dan spinner alangkah baiknya kita mempraktikkanya menggunakan scribt sederhana berikut ini
1. Kita akan membuat tampilan seperti berikut.
2. Buat project baru. Kemudian buat sebuah activity dengan nama MainActivity. Kodingnya adalah sebagai berikut.
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :paddingBottom= "@dimen/activity_vertical_margin" android :paddingLeft= "@dimen/activity_horizontal_margin" android :paddingRight= "@dimen/activity_horizontal_margin" android :paddingTop= "@dimen/activity_vertical_margin" app :layout_behavior= "@string/appbar_scrolling_view_behavior" tools :context= "com.example.lucianugraheni.trylist.MainActivity" tools :showIn="@layout/activity_main"android :orientation= "vertical" >
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:textStyle="bold" android:text= "Selamat datang"/>
<ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listView" android :layout_gravity="center_horizontal"
>Agar kita lebih mengerti tentang penggunaan widget list view dan spinner alangkah baiknya kita mempraktikkanya menggunakan scribt sederhana berikut ini
1. Kita akan membuat tampilan seperti berikut.
2. Buat project baru. Kemudian buat sebuah activity dengan nama MainActivity. Kodingnya adalah sebagai berikut.
<? xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :paddingBottom= "@dimen/activity_vertical_margin" android :paddingLeft= "@dimen/activity_horizontal_margin" android :paddingRight= "@dimen/activity_horizontal_margin" android :paddingTop= "@dimen/activity_vertical_margin" app :layout_behavior= "@string/appbar_scrolling_view_behavior" tools :context= "com.example.lucianugraheni.trylist.MainActivity" tools :showIn="@layout/activity_main"android :orientation= "vertical" >
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:textStyle="bold" android:text= "Selamat datang"/>
</ListView>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal" android :weightSum="1">
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_weight="0.5" android:hint= "Tambahkan hewan"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TAMBAH" android:id="@+id/button" android:layout_gravity="right" android:textStyle= "bold"/>
</LinearLayout>
</LinearLayout>
3. Dan tambahkan pada file ActivityUtama.java sehingga menjadi sebagai berikut
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle; import android.view.KeyEvent; import android.view.View;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity { EditText editText;
Button addButton; TextView textView; SimpleArrayAdapter adapter; ListView listview; ArrayList<String> arrayList; Runnable run;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
addButton = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_gravity="center_horizontal" android :weightSum="1">
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_weight="0.5" android:hint= "Tambahkan hewan"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TAMBAH" android:id="@+id/button" android:layout_gravity="right" android:textStyle= "bold"/>
</LinearLayout>
</LinearLayout>
3. Dan tambahkan pada file ActivityUtama.java sehingga menjadi sebagai berikut
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle; import android.view.KeyEvent; import android.view.View;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity { EditText editText;
Button addButton; TextView textView; SimpleArrayAdapter adapter; ListView listview; ArrayList<String> arrayList; Runnable run;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
..........................
//deklarasi objek layout
addButton = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
listview = (ListView) findViewById(R.id.listView);
String[] hewan = new String[]{"Singa", "Macan", "Buaya", "Serigala", "Badak"};
arrayList = new ArrayList<String>();
for (int i = 0; i < hewan.length; ++i) {
arrayList.add(hewan[i]);
}
adapter = new SimpleArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { final String item =
(String) parent.getItemAtPosition(position);
textView.setText("Yang dipilih: " + item);
}
});
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
tambahDaftar();
}
});
}
private boolean tambahDaftar() { adapter.add(editText.getText().toString()); arrayList.add(editText.getText().toString()); editText.setText(""); adapter.notifyDataSetChanged();
listview.smoothScrollToPosition(adapter.getCount() - 1);
return true;
}
}
4. Buat sebuah java class dengan nama SimpleArrayAdapter.java. Kodingnya sebagai berikut.
import android.content.Context; import android.widget.ArrayAdapter; import java.util.HashMap;
import java.util.List;
public class SimpleArrayAdapter extends ArrayAdapter<String> {
//private static final String TAG = "SimpleArrayAdapter"; Context context;
int textViewResourceId;
HashMap<String, Integer> hashMap = new HashMap<String,
Integer>();
public SimpleArrayAdapter(Context context, int textViewResourceId,
List<String> objects) { super(context, textViewResourceId, objects); this.context = context;
listview = (ListView) findViewById(R.id.listView);
String[] hewan = new String[]{"Singa", "Macan", "Buaya", "Serigala", "Badak"};
arrayList = new ArrayList<String>();
for (int i = 0; i < hewan.length; ++i) {
arrayList.add(hewan[i]);
}
adapter = new SimpleArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) { final String item =
(String) parent.getItemAtPosition(position);
textView.setText("Yang dipilih: " + item);
}
});
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
tambahDaftar();
}
});
}
private boolean tambahDaftar() { adapter.add(editText.getText().toString()); arrayList.add(editText.getText().toString()); editText.setText(""); adapter.notifyDataSetChanged();
listview.smoothScrollToPosition(adapter.getCount() - 1);
return true;
}
}
4. Buat sebuah java class dengan nama SimpleArrayAdapter.java. Kodingnya sebagai berikut.
import android.content.Context; import android.widget.ArrayAdapter; import java.util.HashMap;
import java.util.List;
public class SimpleArrayAdapter extends ArrayAdapter<String> {
//private static final String TAG = "SimpleArrayAdapter"; Context context;
int textViewResourceId;
HashMap<String, Integer> hashMap = new HashMap<String,
Integer>();
public SimpleArrayAdapter(Context context, int textViewResourceId,
List<String> objects) { super(context, textViewResourceId, objects); this.context = context;
this.textViewResourceId = textViewResourceId;
for (int i = 0; i < objects.size(); ++i) {
hashMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return hashMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void add(String object) { hashMap.put(object, hashMap.size()); this.notifyDataSetChanged();
}
}
5. Coba jalankan dan amati hasilnya. Jelaskan jalannya program.
6. Buat project baru. Tuliskan dalam file XML nya layout berikut
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :paddingBottom= "@dimen/activity_vertical_margin" android :paddingLeft= "@dimen/activity_horizontal_margin" android :paddingRight= "@dimen/activity_horizontal_margin" android :paddingTop= "@dimen/activity_vertical_margin" app :layout_behavior= "@string/appbar_scrolling_view_behavior" tools :context= "com.example.lucianugraheni.myapplication.MainActivity" tools :showIn="@layout/activity_main" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pilih Jurusan" android :id="@+id/text1"/>
<Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinerJurusan" android :layout_below="@+id/text1" >
7. Dan dalam file java nya, seperti berikut
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
Spinner spinner;
for (int i = 0; i < objects.size(); ++i) {
hashMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return hashMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public void add(String object) { hashMap.put(object, hashMap.size()); this.notifyDataSetChanged();
}
}
5. Coba jalankan dan amati hasilnya. Jelaskan jalannya program.
6. Buat project baru. Tuliskan dalam file XML nya layout berikut
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android :layout_width= "match_parent" android :layout_height= "match_parent" android :paddingBottom= "@dimen/activity_vertical_margin" android :paddingLeft= "@dimen/activity_horizontal_margin" android :paddingRight= "@dimen/activity_horizontal_margin" android :paddingTop= "@dimen/activity_vertical_margin" app :layout_behavior= "@string/appbar_scrolling_view_behavior" tools :context= "com.example.lucianugraheni.myapplication.MainActivity" tools :showIn="@layout/activity_main" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pilih Jurusan" android :id="@+id/text1"/>
<Spinner android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/spinerJurusan" android :layout_below="@+id/text1" >
</Spinner>
</RelativeLayout>
7. Dan dalam file java nya, seperti berikut
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
..............................
spinner = (Spinner) findViewById(R.id.spinerJurusan);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter. createFromResource(this, R.array.jurusan, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout. simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view,
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
......................
}
8. Pada resource string
<resources>
..........................
<string-array name= "jurusan">
<item>Teknik Informatika</ item>
<item>Sistem Informasi</item >
<item>Manajemen Informatika</ item>
<item>Komputerisasi Akuntansi</ item>
<item>Teknik Komputer</item >
</string-array>
</resources>
9. Jalankan dan amati hasilnya.
10. Buat project baru lagi. Kemudian Buat 3 buah file XML berikut.
activity_main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ExpandableListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/expList" android:indicatorLeft="?android:
attr/expandableListPreferredItemIndicatorLeft" android:divider="#A4C739" android:dividerHeight="0.5dp">
</ExpandableListView>
protected void onCreate(Bundle savedInstanceState) {
..............................
spinner = (Spinner) findViewById(R.id.spinerJurusan);
spinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter. createFromResource(this, R.array.jurusan, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout. simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view,
int
pos, long id) {
String hasil = spinner.getSelectedItem().toString();
Toast.makeText(this,"Anda memilih "+hasil, Toast. LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
......................
}
8. Pada resource string
<resources>
..........................
<string-array name= "jurusan">
<item>Teknik Informatika</ item>
<item>Sistem Informasi</item >
<item>Manajemen Informatika</ item>
<item>Komputerisasi Akuntansi</ item>
<item>Teknik Komputer</item >
</string-array>
</resources>
9. Jalankan dan amati hasilnya.
10. Buat project baru lagi. Kemudian Buat 3 buah file XML berikut.
activity_main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ExpandableListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/expList" android:indicatorLeft="?android:
attr/expandableListPreferredItemIndicatorLeft" android:divider="#A4C739" android:dividerHeight="0.5dp">
</ExpandableListView>
</RelativeLayout>
parent_layout.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">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/parent_txt" android:paddingLeft="?android:
attr/expandableListPreferredItemPaddingLeft" android:paddingTop="10dp" android:paddingBottom="10dp"/>
</LinearLayout>
child_layout.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">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/child_txt" android:paddingLeft="?android:
attr/expandableListPreferredItemPaddingLeft" android:paddingTop="10dp" android:paddingBottom="10dp"/>
</LinearLayout>
11. Buat kelas juga. Pertama, ubah MainActivity.java sehingga menjadi sebagai berikut.
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;
import android.widget.ExpandableListView;
import java.util.ArrayList; import java.util.HashMap; import java.util.List;
public class MainActivity extends Activity { HashMap<String, List<String>> Movies_category; List<String> Movies_list;
ExpandableListView Exp_list; MoviesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
parent_layout.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">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/parent_txt" android:paddingLeft="?android:
attr/expandableListPreferredItemPaddingLeft" android:paddingTop="10dp" android:paddingBottom="10dp"/>
</LinearLayout>
child_layout.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">
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/child_txt" android:paddingLeft="?android:
attr/expandableListPreferredItemPaddingLeft" android:paddingTop="10dp" android:paddingBottom="10dp"/>
</LinearLayout>
11. Buat kelas juga. Pertama, ubah MainActivity.java sehingga menjadi sebagai berikut.
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;
import android.widget.ExpandableListView;
import java.util.ArrayList; import java.util.HashMap; import java.util.List;
public class MainActivity extends Activity { HashMap<String, List<String>> Movies_category; List<String> Movies_list;
ExpandableListView Exp_list; MoviesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Exp_list = (ExpandableListView)findViewById(R.id.expList);
Movies_category = DataProvider.getInfo();
Movies_list = new ArrayList<String> (Movies_category.keySet());
adapter = new MoviesAdapter(this,Movies_category, Movies_list);
Exp_list.setAdapter(adapter);
}
......koding yang sudah ada.
}
import android.content.Context; import android.graphics.Typeface; import android.view.LayoutInflater; import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MoviesAdapter extends BaseExpandableListAdapter {
private Context ctx;
private HashMap<String, List<String>> Movies_Category;
private List<String> Movies_List;
public MoviesAdapter(Context ctx, HashMap<String, List<String>> Movies_Category,
List<String> Movies_List){
this.ctx = ctx;
this.Movies_Category = Movies_Category;
this.Movies_List = Movies_List;
}
@Override
public int getGroupCount() {
return Movies_List.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return Movies_Category.get(Movies_List. get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return Movies_List.get(groupPosition);
}
@Override
public Object getChild(int parent, int child) {
}
Movies_list = new ArrayList<String> (Movies_category.keySet());
adapter = new MoviesAdapter(this,Movies_category, Movies_list);
Exp_list.setAdapter(adapter);
}
......koding yang sudah ada.
}
12. Kemudian buat 2 buah file java. MoviesAdapter.java dan
DataProvider.java
M
oviesAdapter.java
import android.content.Context; import android.graphics.Typeface; import android.view.LayoutInflater; import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class MoviesAdapter extends BaseExpandableListAdapter {
private Context ctx;
private HashMap<String, List<String>> Movies_Category;
private List<String> Movies_List;
public MoviesAdapter(Context ctx, HashMap<String, List<String>> Movies_Category,
List<String> Movies_List){
this.ctx = ctx;
this.Movies_Category = Movies_Category;
this.Movies_List = Movies_List;
}
@Override
public int getGroupCount() {
return Movies_List.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return Movies_Category.get(Movies_List. get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return Movies_List.get(groupPosition);
}
@Override
public Object getChild(int parent, int child) {
return Movies_Category.get(Movies_List.
get(parent)).get(child);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int parent, int child) {
return child;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView) {
String groupTitle = (String) getGroup(parent);
if (convertView==null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.parent_layout,
parentView,false);
}
TextView parentTextView = (TextView)
convertView.findViewById(R.id.parent_txt); parentTextView.setTypeface(null, Typeface.BOLD); parentTextView.setText(groupTitle);
return convertView;
}
@Override
public View getChildView(int parent, int child, boolean isLastChild, View convertView,
ViewGroup parentView) { String childTitle = (String) getChild(parent, child); if (convertView==null) {
LayoutInflater inflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, parentView, false);
}
TextView childTextView = (TextView)
convertView.findViewById(R.id.child_txt);
childTextView.setText(childTitle);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return false;}
}
D ataProvider.java
import java.util.ArrayList;
import java.util.HashMap;
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int parent, int child) {
return child;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int parent, boolean isExpanded, View convertView, ViewGroup parentView) {
String groupTitle = (String) getGroup(parent);
if (convertView==null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.parent_layout,
parentView,false);
}
TextView parentTextView = (TextView)
convertView.findViewById(R.id.parent_txt); parentTextView.setTypeface(null, Typeface.BOLD); parentTextView.setText(groupTitle);
return convertView;
}
@Override
public View getChildView(int parent, int child, boolean isLastChild, View convertView,
ViewGroup parentView) { String childTitle = (String) getChild(parent, child); if (convertView==null) {
LayoutInflater inflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_layout, parentView, false);
}
TextView childTextView = (TextView)
convertView.findViewById(R.id.child_txt);
childTextView.setText(childTitle);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition,
int childPosition) {
return false;}
}
D ataProvider.java
import java.util.ArrayList;
import java.util.List;
public class DataProvider {
public static HashMap<String, List<String>> getInfo(){ HashMap<String, List<String>> MoviesDetails =
new HashMap<String, List<String>>(); List<String> Action_Movies = new ArrayList<>(); Action_Movies.add("AA");
Action_Movies.add("BB"); Action_Movies.add("CC"); Action_Movies.add("DD"); Action_Movies.add("EE");
List<String> Romantic_Movies = new ArrayList<>(); Romantic_Movies.add("FF"); Romantic_Movies.add("GG"); Romantic_Movies.add("HH"); Romantic_Movies.add("II"); Romantic_Movies.add("JJ");
List<String> Horror_Movies = new ArrayList<>(); Horror_Movies.add("KK");
Horror_Movies.add("LL"); Horror_Movies.add("MM"); Horror_Movies.add("NN"); Horror_Movies.add("OO");
List<String> Comedy_Movies = new ArrayList<>(); Comedy_Movies.add("PP");
Comedy_Movies.add("QQ"); Comedy_Movies.add("RR"); Comedy_Movies.add("SS"); Comedy_Movies.add("TT");
MoviesDetails.put("Action Movies", Action_Movies); MoviesDetails.put("Romantics Movies", Romantic_Movies); MoviesDetails.put("Horror Movies", Horror_Movies); MoviesDetails.put("Comedy Movies", Comedy_Movies); return MoviesDetails;
}
}
13. Jalankan dan amati hasilnya.
0 Response to "ListView dan Spinner pada Android Studio"
Post a Comment