Friday, January 31, 2014

Android ListView Tutorial

Berikut adalah activity_main.xml

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List View Example" />

    <ListView
        android:id="@+id/lv_example"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>


</LinearLayout>

Buat Android XML file custom_item.xml

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="List View Example" />

    <ListView
        android:id="@+id/lv_example"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>


</LinearLayout>

Buat class Model.java dalam folder src/<nama package Anda>

public class Model {

int id;
String nama;
int image;

public Model() {
}

public Model(int id, String nama, int image) {
super();
this.id = id;
this.nama = nama;
this.image = image;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public int getImage() {
return image;
}

public void setImage(int image) {
this.image = image;
}

}

Buat class Adapter.java dalam folder src/<nama package Anda>

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Adapter extends ArrayAdapter<String> {
private Context context;
private int custom_resources;
private String[] ids;
private ArrayList<Model> models;
public Adapter(Context context, int custom_resources, String[] ids, ArrayList<Model> models) {
super(context, custom_resources, ids);
this.context = context;
this.custom_resources = custom_resources;
this.ids = ids;
this.models = models;
}
public View getView(int position, View convertView, ViewGroup group) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(custom_resources, group, false);
ImageView iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
TextView tv_nama = (TextView) convertView.findViewById(R.id.tv_name);
int id = Integer.parseInt(ids[position]);
for (int x = 0; x < models.size(); x++) {
if (id == models.get(x).getId()) {
iv_image.setImageResource(models.get(x).getImage());
tv_nama.setText(models.get(x).getNama());
Model model = new Model(models.get(x).getId(), models.get(x).getNama(), models.get(x).getImage());
tv_nama.setTag(model);
break;
}
}
return convertView;
}

}

Berikut adalah code dalam MainActivity.java

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
ListView lv_example;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_example = (ListView) findViewById(R.id.lv_example);
showList();
lv_example.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> adapter, View view, int selected, long arg3) {
String text = ((Model) view.findViewById(R.id.tv_name).getTag()).getNama();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void showList() {
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model(0, "Angky Cahaya Putra", R.drawable.ic_launcher));
models.add(new Model(1, "08968xxxxxxx", R.drawable.ic_launcher));
models.add(new Model(2, "SEAMOLEC Tutorial", R.drawable.ic_launcher));
String[] ids = new String[models.size()];
for (int x = 0; x < models.size(); x++) {
ids[x] = Integer.toString(models.get(x).getId());
}
Adapter adapter = new Adapter(getApplicationContext(), R.layout.custom_item, ids, models);
lv_example.setAdapter(adapter);
}

}

No comments:

Post a Comment