我正在开发一个应用程序,用户可以通过AutocompleteTextView搜索他们的手机联系人,然后将这些联系人添加到ListView。到目前为止,我已经能够做到这一点,但现在我想限制用户可以添加到五个的联系人数量。这样只会有五个联系人。如何将限制设置为5,如果用户尝试上传更多内容,则显示警告对话框?
这是我的主要java代码,用于读取用户手机联系人,并在点击“添加联系人”按钮后将其添加到列表视图中。
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);
}
}
}
这是我的主要xml页面,其中包含AutoCompleteTextView,button和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>
这是我自动完成文本视图的自定义视图的xml
<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>
答案 0 :(得分:0)
致电.setDropDownHeight(int height)