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:
- 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;
}
}
}
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:orientation="vertical"
- 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="com.example.contactlist.MainActivity$PlaceholderFragment" >
- <Button
- android:id="@+id/btnOk"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="OK" />
- </LinearLayout>
- 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à:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/LinearLayout1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- 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="com.example.contactlist.ContactList$PlaceholderFragment" >
- <ListView
- android:id="@+id/lstContact"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- </ListView>
- </LinearLayout>
- package com.example.contactlist;
- import common.CommonKeys;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class ContactDetailActivity extends Activity {
- private ImageView imv_Contact;
- private TextView txt_name;
- private TextView txt_number;
- private void init() {
- imv_Contact = (ImageView) findViewById(R.id.imvContact);
- txt_name = (TextView) findViewById(R.id.txtName);
- txt_number = (TextView) findViewById(R.id.txtNumber);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_detail);
- init();
- Bundle extras = getIntent().getExtras();
- txt_name.setText(extras.getString(CommonKeys.CONCTACT_NAME));
- txt_number.setText(extras.getString(CommonKeys.CONCTACT_NUMBER));
- imv_Contact.setImageResource(extras.getInt(CommonKeys.CONCTACT_PHOTO));
- }
- }
File CommonKeys.java code là: chí ý file này nằm trong gói Common nhé
- package common;
- public class CommonKeys {
- public static final String CONCTACT_NAME = "contact_nam_key";
- public static final String CONCTACT_NUMBER = "contact_number_key";
- public static final String CONCTACT_PHOTO = "contact_photo_key";
- }
- <?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à:
- <?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"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/imvContact"
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:src="@drawable/ic_launcher" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- 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>
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