Tutorial Programming

Tutorial Menampilkan ListView Picture


Hallo sahabat saya akan menjelaskan tentang bagaimana menampilkan picture di ListView nah langsung aja kita lihat di TKP

1. Buatlah project android kalian

2. Lalu copy coding MainActivity.java tersebut

package com.example.tutorlistpic;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView lvcountry;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		lvcountry = (ListView) findViewById(R.id.listCountry);

		List<Country> lsCountry = new ArrayList<Country>();

		lsCountry
				.add(new Country(BitmapFactory.decodeResource(getResources(),
						R.drawable.indonesia), "Indonesia", "Jakarta",
						"246 juta jiwa "));
		lsCountry.add(new Country(BitmapFactory.decodeResource(getResources(),
				R.drawable.saudi), "Saudi Arabia ", "Riyadh", "28 juta jiwa"));
		lsCountry.add(new Country(BitmapFactory.decodeResource(getResources(),
				R.drawable.inggris), "Inggris", "London", "53 juta jiwa"));
		lsCountry.add(new Country(BitmapFactory.decodeResource(getResources(),
				R.drawable.us), "Amerika serikat", "Washingthon DC",
				"313 juta jiwa"));
		lsCountry.add(new Country(BitmapFactory.decodeResource(getResources(),
				R.drawable.china), "China", "Beijing", "1.35 Milyar jiwa"));

		CountryAdapter adapter = new CountryAdapter(this, lsCountry);
		lvcountry.setAdapter(adapter);
	}
}

3. Copy coding Country.java tersebut

package com.example.tutorlistpic;

import android.graphics.Bitmap;

public class Country {
	private Bitmap flagIcon;

	private String nama;
	private String ibukota;
	private String jumlahPenduduk;
	
	public Country(Bitmap fi, String n, String i, String jp) {
		flagIcon = fi;
		nama = n;
		ibukota = i;
		jumlahPenduduk = jp;
	}
	
	public Bitmap getFlagIcon() {
		return flagIcon;
	}

	public void setFlagIcon(Bitmap flagIcon) {
		this.flagIcon = flagIcon;
	}

	public String getNama() {
		return nama;
	}

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

	public String getIbukota() {
		return ibukota;
	}

	public void setIbukota(String ibukota) {
		this.ibukota = ibukota;
	}

	public String getJumlahPenduduk() {
		return jumlahPenduduk;
	}

	public void setJumlahPenduduk(String jumlahPenduduk) {
		this.jumlahPenduduk = jumlahPenduduk;
	}

	
	
}

4. Copy coding CountryAdapter.java tersebut

package com.example.tutorlistpic;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CountryAdapter extends BaseAdapter {
	private Context mContext;
	private List<Country> mListInfo;

	public CountryAdapter(Context context, List<Country> list) {
		mContext = context;
		mListInfo = list;

	}

	@Override
	public int getCount() {
		return mListInfo.size();
	}

	@Override
	public Object getItem(int pos) {
		return mListInfo.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		// get selected entry
		Country entry = mListInfo.get(pos);

		// inflating list view layout if null
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.country_row, null);
		}

		// set image icon
		ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imgIcon);
		imgIcon.setImageBitmap(entry.getFlagIcon());

		// set nama
		TextView tvName = (TextView) convertView.findViewById(R.id.tvNama);
		tvName.setText(entry.getNama());

		TextView tvPhone = (TextView) convertView.findViewById(R.id.tvIbukota);
		tvPhone.setText(entry.getIbukota());

		TextView tvJumlahPenduduk = (TextView) convertView
				.findViewById(R.id.tvJumlahPenduduk);
		tvJumlahPenduduk.setText(entry.getJumlahPenduduk());

		return convertView;
	}

}

5. Setelah itu buat pada folder drawable-hdpi anda harus memasukan picture yang anda butuhkan dengan menyesuaikan namanya

6. Lalu copy coding activity_main.xml tersebut

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listCountry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

7. Lalu copy coding country_row.xml tersebut

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="70dip"
        android:layout_height="70dip"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvNama"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvIbukota"
            android:layout_width="fill_parent"
            android:textSize="18sp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/tvJumlahPenduduk"
            android:layout_width="fill_parent"
            android:textStyle="italic"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>


8. Lalu run aplikasi kalian.

9. Oke seleseai project kalian sahabat.





Silakan download project aplikasi disini


Sekian tutorial saya semoga bermanfaat

No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.