Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
}
}
<?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>
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;
}
}
<?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