Created
March 5, 2012 02:07
-
-
Save joshdholtz/1976022 to your computer and use it in GitHub Desktop.
Android - Custom List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ContactsActivity extends Activity { | |
List<Contact> contacts; | |
ContactsAdapter contactsAdapter; | |
ListView contactsList; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_contacts); | |
contactsAdapter = new ContactsAdapter(this, contacts); | |
contactsList = (ListView) this.findViewById(R.id.contacts_lst_contacts); | |
contactsList.setOnItemClickListener(contactOnItemClickListener); | |
contactsList.setAdapter(contactsAdapter); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="vertical" | |
android:background="@drawable/app_background" > | |
<TextView android:layout_width="fill_parent" | |
android:layout_height="50dp" | |
android:background="@drawable/action_bar_gradient" | |
android:gravity="center" | |
android:textColor="#FFFFFF" | |
android:textSize="28sp" | |
android:textStyle="bold" | |
android:text="Contacts" /> | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" | |
android:padding="10dp" > | |
<LinearLayout | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:background="@drawable/rounded_background" | |
android:padding="10dp" > | |
<ListView android:id="@+id/contacts_lst_contacts" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> | |
</LinearLayout> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ContactsAdapter extends BaseAdapter { | |
private Context context; | |
private List<Contact> contacts; | |
public ContactsAdapter(Context context, List<Contact> contacts) { | |
this.context = context; | |
this.contacts = contacts; | |
} | |
public int getCount() { | |
return contacts.size(); | |
} | |
public Contact getItem(int position) { | |
return contacts.get(position); | |
} | |
public long getItemId(int position) { | |
return 0; | |
} | |
public View getView(int position, View convertView, ViewGroup parent) { | |
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View view = vi.inflate(R.layout.list_item_contact, null); | |
Contact contact = contacts.get(position); | |
if (contact != null) { | |
TextView txtFullName = (TextView) view.findViewById(R.id.list_item_contact_txt_full_name); | |
TextView txtPosition = (TextView) view.findViewById(R.id.list_item_contact_txt_position); | |
txtFullName.setText(contact.getFullName()); | |
txtPosition.setText(contact.getPosition()); | |
} | |
return view; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:padding="10dp" | |
android:orientation="vertical" | |
android:background="#FFFFFF" > | |
<TextView android:id="@+id/list_item_contact_txt_full_name" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textColor="#000000" | |
android:textSize="20sp" | |
android:textStyle="bold" /> | |
<TextView android:id="@+id/list_item_contact_txt_position" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textColor="#000000" | |
android:textSize="16sp"/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment