Judul : Tutorial Listview Android
link : Tutorial Listview Android
Tutorial Listview Android
Tutorial Listview Android
Berikut cara membuat Listview android dengan Eclipse.
Download SOURCE CODE
1. Listview Activity
MainActivity.java
package com.akatog.android;
import com.akatog.android.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends ListActivity {
static final String[] SOSIAL_MEDIA = new String[] { "Facebook", "Twitter",
"Path", "Youtube"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
// R.id.label, SOSIAL_MEDIA));
setListAdapter(new MobileArrayAdapter(this, SOSIAL_MEDIA));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
import com.akatog.android.MobileArrayAdapter;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends ListActivity {
static final String[] SOSIAL_MEDIA = new String[] { "Facebook", "Twitter",
"Path", "Youtube"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
// R.id.label, SOSIAL_MEDIA));
setListAdapter(new MobileArrayAdapter(this, SOSIAL_MEDIA));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
}
MobileArrayAdapter.java
package com.akatog.android;
import com.akatog.android.R;
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 MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.main, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("Twitter")) {
imageView.setImageResource(R.drawable.twitter_icon);
} else if (s.equals("Path")) {
imageView.setImageResource(R.drawable.path_icon);
} else if (s.equals("Youtube")) {
imageView.setImageResource(R.drawable.youtube_icon);
} else {
imageView.setImageResource(R.drawable.facebook_icon);
}
return rowView;
}
}
import com.akatog.android.R;
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 MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MobileArrayAdapter(Context context, String[] values) {
super(context, R.layout.main, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("Twitter")) {
imageView.setImageResource(R.drawable.twitter_icon);
} else if (s.equals("Path")) {
imageView.setImageResource(R.drawable.path_icon);
} else if (s.equals("Youtube")) {
imageView.setImageResource(R.drawable.youtube_icon);
} else {
imageView.setImageResource(R.drawable.facebook_icon);
}
return rowView;
}
}
2. Listview Layout
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/icon"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="@drawable/facebook_icon" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="@+id/icon"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="@drawable/facebook_icon" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="30px" >
</TextView>
</LinearLayout>
- Untuk hasilnya bisa lihat Video di atas
- Download Sourcenya dan Import di Eclips
SELAMAT BELAJAR DAN MENCOBA
Demikianlah Artikel Tutorial Listview Android
Sekianlah artikel Tutorial Listview Android kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.
Anda sekarang membaca artikel Tutorial Listview Android dengan alamat link http://akatog.blogspot.com/2015/02/tutorial-listview-android.html
0 comments:
Post a Comment