Tutorial Programming

Tutorial ListView Detail On Clik

Hallo sahabat yang baca tutorial saya, saya akan mengajarkan bagaimana cara membuat listView dan saat kita klik lalu muncul detail info dari list tersebut, oke kita langsung aja

1. Buatlah project android kalian
2. Copy coding MainActivity.java

package com.example.list;


import android.os.Bundle;

import android.app.Activity;

import android.app.ProgressDialog;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.AdapterView.OnItemClickListener;
 


public class MainActivity extends Activity{
 
 
 private ListView listView;
 
 private ArrayAdapter<String> arrayAdapter;
 
 private String[] programmingLang={
   
 "Eko", "Jaya","Fazri", "Ari","Andi", "Wong","Asli", "Palembang"
 
 };
 
    
 @Override
    
 protected void onCreate(Bundle savedInstanceState) {
    
    
  super.onCreate(savedInstanceState);
        
  setContentView(R.layout.activity_main);
        
        
  listView = (ListView) findViewById(R.id.listViewProgramming1);
        
  arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,programmingLang);
        
  listView.setAdapter(arrayAdapter);    
        
        
  listView.setOnItemClickListener(new OnItemClickListener() {
  
          
            
  public void onItemClick(AdapterView<?> parent, View view,
              
                
   int position, long id) {
                
   String detail = listView.getItemAtPosition(position).toString();
                
              
             
   Intent intent= new Intent(getApplicationContext(),DetailListView.class);
                
   intent.putExtra("detail", detail);           
                
   startActivity(intent);          
             
   }
          
  });
    
 }
 

}

2. Lalu create DetailListView.java dan Copy coding tersbut

package com.example.list;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
 
public class DetailListView extends Activity{
 String detail;
 TextView NamaDetail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);
        
        NamaDetail=(TextView) findViewById(R.id.IdDetail);
        
        // ambil parameter nama_tahun dari intent 
        Intent intent = getIntent();
        detail = intent.getStringExtra("detail");
     
        // tampilkan nama tahun di TextView nama tahun 
        NamaDetail.setText(detail);
    }
 
}

3. Copy coding 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ListActivity" >

    <ListView
        android:id="@+id/listViewProgramming1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" >

    </ListView>
  
</RelativeLayout>

4. Copy coding detail.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".ListActivity" >

    <TextView
        android:id="@+id/IdDetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="95dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
  
</RelativeLayout>

5. Copy coding AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.list"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.list.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DetailListView">
        </activity>
    </application>

</manifest>

6. Lalu run aplikasi

7. Selesai aplikasi project android anda.




Semoga tutorial saya bermafaat


1 comment:

  1. makasih ilmunya, mohon di kasih video tutorialnya agar lebih mudah.

    ReplyDelete

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.