I am developing an application where a user will be able to search their phone contacts through an AutocompleteTextView, then add those contacts to a ListView. So far I have been able to do this, but now I want to limit the amount of contacts the user can add to five. This way there will only be five contacts. How can I set the limit to five, and display an alert dialog if the user tries to upload more?
Here is my main java code that reads the users phone contacts and adds them to the listview after clicking the "add contact" button.
package com.example.app;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
final ArrayList<String> phoneList = new ArrayList<String>();
FancyAdapter aa=null;
//Sets contact array for autocomplete
private ArrayList<Map<String, String>> mPeopleList;
private SimpleAdapter mAdapter;
private AutoCompleteTextView mTxtPhoneNo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPeopleList = new ArrayList<Map<String, String>>();
PopulatePeopleList();
mTxtPhoneNo = (AutoCompleteTextView) findViewById(R.id.mmWhoNo);
mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.custcontview ,new String[] { "Name", "Phone" , "Type" }, new int[] { R.id.ccontName, R.id.ccontNo, R.id.ccontType });
mTxtPhoneNo.setAdapter(mAdapter);
mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View arg1, int index,
long arg3) {
Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
String name = map.get("Name");
String number = map.get("Phone");
mTxtPhoneNo.setText(""+name+"\n"+number+"");
}
});
}
public void PopulatePeopleList()
{
mPeopleList.clear();
Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext())
{
String contactName = people.getString(people.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ((Integer.parseInt(hasPhone) > 0))
{
// You now have the number so now query it like this
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);
while (phones.moveToNext()) {
//store numbers and display a dialog letting the user select which.
String phoneNumber = phones.getString(
phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
String numberType = phones.getString(phones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.TYPE));
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Name", contactName);
NamePhoneType.put("Phone", phoneNumber);
if(numberType.equals("0"))
NamePhoneType.put("Type", "Work");
else
if(numberType.equals("1"))
NamePhoneType.put("Type", "Home");
else if(numberType.equals("2"))
NamePhoneType.put("Type", "Mobile");
else
NamePhoneType.put("Type", "Other");
//Then add this map to the list.
mPeopleList.add(NamePhoneType);
}
phones.close();
}
}
people.close();
aa=new FancyAdapter();
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText mmWhoNo = (EditText)findViewById(R.id.mmWhoNo);
myListView.setAdapter(aa);
Button btnSimple = (Button) findViewById(R.id.btnSimple);
btnSimple.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//adds contact
phoneList.add(0, mmWhoNo.getText().toString());
//update the view
aa.notifyDataSetChanged();
}
});
}
//ArrayAdapter is extended and this allows custom views and other more complicated functions
class FancyAdapter extends ArrayAdapter<String> {
FancyAdapter() {
super(MainActivity.this, android.R.layout.simple_list_item_1, phoneList);
}
public View getView(int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
//the if statement checks for recycled items and saves resources and processing
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.custom_list_item, null);
holder=new ViewHolder(convertView);
//caches the result of the findViewById function
holder.note = (TextView) convertView.findViewById(R.id.ccontName);
//stores tag on the view
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.note.setText(phoneList.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView note=null;
ViewHolder(View row) {
note=(TextView)row.findViewById(R.id.ccontName);
}
void populateFrom(String r) {
note.setText(r);
}
}
}
Here is my main xml page with the AutoCompleteTextView, button, and listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<AutoCompleteTextView
android:id="@+id/mmWhoNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type Here to Add an Emergency Contact"
>
</AutoCompleteTextView>
<Button
android:id="@+id/btnSimple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/mmWhoNo"
android:text="Add Contact" />
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/btnSimple" >
</ListView>
</RelativeLayout>
Here is my xml for my custom listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/imageView01"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/ccontName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/ccontName"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/imageView01"
android:gravity="center_vertical"
android:text="Example application"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
And here is the xml for my custom view for the AutoCompleteTextView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/ccontName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#A5000000"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/ccontNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/ccontName"
android:text="Medium Text"
android:textColor="#A5000000"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/ccontType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/ccontNo"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Small Text"
android:textColor="#A5000000"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>