300x250 AD TOP

Tìm kiếm Blog này

Thứ Năm, 26 tháng 6, 2014

Tag:

Hiển thị contactList Buổi 2 Android - ListView

Chào các bạn !
Hôm nay mình sẽ hưỡng dẫn các bạn làm tiếp phần ContactList nhưng được thêm phần ảnh vào !
- Thứ nhất phần thiết kế Activity mình không nói qua nữa nhé coi như các bạn làm được nhé ! nếu phần này còn không hiểu thì comment để lại skype mình sẽ hướng dẫn qua skype nhé !

Bắt đâu các bạn tạo cho mình các file java và file Activity như sau nhé:


Kết quả sau khi chạy sẽ là như thế này nhé:


Ok @-@ !

Bắt đâu Code nhé :

Ở file MainActivity.java  mình code như sau: 

  1. package com.example.contactlist;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;

    public class MainActivity extends Activity {
        private Button btn_Ok;

        private void init() {
            btn_Ok = (Button) findViewById(R.id.btnOk);
            btn_Ok.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, ContactList.class);
                    startActivity(intent);
                }
            });
        }

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

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.activity_main, container,
                        false);
                return rootView;
            }
        }

    }
Ở file activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:id="@+id/list"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:gravity="center"
  7.     android:orientation="vertical"
  8.     android:paddingBottom="@dimen/activity_vertical_margin"
  9.     android:paddingLeft="@dimen/activity_horizontal_margin"
  10.     android:paddingRight="@dimen/activity_horizontal_margin"
  11.     android:paddingTop="@dimen/activity_vertical_margin"
  12.     tools:context="com.example.contactlist.MainActivity$PlaceholderFragment" >
  13.     <Button
  14.         android:id="@+id/btnOk"
  15.         android:layout_width="fill_parent"
  16.         android:layout_height="wrap_content"
  17.         android:text="OK" />
  18. </LinearLayout>
File ContactList.java mình code là:

  1. package com.example.contactlist;

    import common.CommonKeys;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;

    public class ContactList extends Activity {
        private ListView lst_Contact;
        private String [] dataName = {
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
                "Hai Lam it 1", " Blog Hai Lam it", "hailamit 2", "Blog Hai lam it 2", "Cuoc song tuoi dep",
               
        };
       
        private String [] dataNumber = {
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
                "0167.7575.999", " 0983.24.26.85", "0123456798", "09876543212", "0987654334",
        };
        private int [] dataPhoto = {
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
                R.drawable.anh1, R.drawable.anh2, R.drawable.anh3 , R.drawable.anh4, R.drawable.anh5,
        };
       
       
       
        private void init(){
            lst_Contact=(ListView) findViewById(R.id.lstContact);
           
            lst_Contact.setAdapter(new MylistAdapter());
        }
       

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_contact_list);
            init();
           
        }
        class MylistAdapter extends BaseAdapter{

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return dataName.length;
            }

            @Override
            public Object getItem(int hailamit) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int hailamit) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(final int hailamit, View arg1, ViewGroup arg2) {
               
                LayoutInflater inflater = getLayoutInflater();
                //Bơm layout vao cai View mình tao ở dưới
                View contactitemview = inflater.inflate(R.layout.contact_list_item, null);
               
                TextView textname = (TextView) contactitemview.findViewById(R.id.txtName);
                textname.setText(dataName[hailamit]);
               
                TextView textnumber = (TextView) contactitemview.findViewById(R.id.txtNumber);
                textnumber.setText(dataNumber[hailamit]);
               
                ImageView textphoto = (ImageView) contactitemview.findViewById(R.id.imvContact);
                textphoto.setImageResource(dataPhoto[hailamit]);
               
               
                textname.setOnClickListener(new OnClickListener() {
                   
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(ContactList.this, ContactDetailActivity.class);
                       
                        Bundle extras = new Bundle();
                       
                        extras.putString(CommonKeys.CONCTACT_NAME, dataName[hailamit]);
                        extras.putString(CommonKeys.CONCTACT_NUMBER, dataNumber[hailamit]);
                        extras.putLong(CommonKeys.CONCTACT_PHOTO, dataPhoto[hailamit]);
                       
                        intent.putExtras(extras);
                       
                       
                        startActivity(intent);
                    }
                });
                return contactitemview;
            }
           
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.contact_list, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.activity_contact_list,
                        container, false);
                return rootView;
            }
        }

    }

File activity_contact_list.xml  nội dung code là: 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:id="@+id/LinearLayout1"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical"
  7.     android:paddingBottom="@dimen/activity_vertical_margin"
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"
  9.     android:paddingRight="@dimen/activity_horizontal_margin"
  10.     android:paddingTop="@dimen/activity_vertical_margin"
  11.     tools:context="com.example.contactlist.ContactList$PlaceholderFragment" >
  12.     <ListView
  13.         android:id="@+id/lstContact"
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content" >
  16.     </ListView>
  17. </LinearLayout>
File Java ContactDetailActivity.java Code là:

  1. package com.example.contactlist;
  2. import common.CommonKeys;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.widget.ImageView;
  6. import android.widget.TextView;
  7. public class ContactDetailActivity extends Activity {
  8.     private ImageView imv_Contact;
  9.     private TextView txt_name;
  10.     private TextView txt_number;
  11.     private void init() {
  12.         imv_Contact = (ImageView) findViewById(R.id.imvContact);
  13.         txt_name = (TextView) findViewById(R.id.txtName);
  14.         txt_number = (TextView) findViewById(R.id.txtNumber);
  15.     }
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_detail);
  20.         init();
  21.        
  22.         Bundle extras = getIntent().getExtras();
  23.         txt_name.setText(extras.getString(CommonKeys.CONCTACT_NAME));
  24.         txt_number.setText(extras.getString(CommonKeys.CONCTACT_NUMBER));
  25.         imv_Contact.setImageResource(extras.getInt(CommonKeys.CONCTACT_PHOTO));
  26.     }
  27. }

 File CommonKeys.java code là: chí ý file này nằm trong gói Common nhé

  1. package common;
  2. public class CommonKeys {
  3.     public static final String CONCTACT_NAME = "contact_nam_key";
  4.     public static final String CONCTACT_NUMBER = "contact_number_key";
  5.     public static final String CONCTACT_PHOTO = "contact_photo_key";
  6. }
File activity_detail.xml  mình code là:

  1. <?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"
        android:gravity="center|top"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imvContact"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:gravity="center|top"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/txtName"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Large Text"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/txtNumber"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>

    </LinearLayout>

File contact_list_item.xml code là:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:gravity="center"
  6.     android:orientation="horizontal" >
  7.     <ImageView
  8.         android:id="@+id/imvContact"
  9.         android:layout_width="80dp"
  10.         android:layout_height="80dp"
  11.         android:src="@drawable/ic_launcher" />
  12.     <LinearLayout
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="match_parent"
  15.         android:gravity="center"
  16.         android:orientation="vertical" >
  17.         <TextView
  18.             android:id="@+id/txtName"
  19.             android:layout_width="fill_parent"
  20.             android:layout_height="wrap_content"
  21.             android:text="Large Text"
  22.             android:textAppearance="?android:attr/textAppearanceLarge" />
  23.         <TextView
  24.             android:id="@+id/txtNumber"
  25.             android:layout_width="fill_parent"
  26.             android:layout_height="wrap_content"
  27.             android:text="Medium Text"
  28.             android:textAppearance="?android:attr/textAppearanceMedium" />
  29.     </LinearLayout>
  30. </LinearLayout>
 Ok ! vậy toàn bộ code mình đã share cho bạn rồi !
Bài sau mình sẽ hướng dẫn tối ưu code của thằng listview !

0 nhận xét:

Đăng nhận xét