1.用来保存对象
private List<View> listOfPhoneSubView;//存放子View对象
private List<Button> listOfPhoneButton;//左边的按钮,用来选择类型,文本的获取也是通过这里
private List<EditText> listOfPhoneEditText;//右边的文本框,用来获取电话号码
private List<String> listOfPhoneString;//用来保存左边按钮被点击的时候需要传递到别的Activity并显示的字符
private List<Object> listOfPhoneTag;//用来标记对象
private static List<HashMap<String, String>> listOfResolvedPhone;//存放从系统中解析出来的电话信息
private int tagFlag = 0;
2.初始化
private void initList()//初始化ArrayList
{
listOfPhoneSubView = new ArrayList<View>();
listOfPhoneButton = new ArrayList<Button>();
listOfPhoneEditText = new ArrayList<EditText>();
listOfPhoneString = new ArrayList<String>();
listOfPhoneTag = new ArrayList<Object>();
}
private final String string1 = "住宅";//这个不要这样做,可以去读取系统的
private final String string2 = "手机";
private final String string3 = "单位";
private final String string4 = "单位传真";
private final String string5 = "住宅传真";
private final String string6 = "寻呼机";
private final String string7 = "其他";
private final String string8 = "回拨号码";
private final String string9 = "车载电话";
private final String string10 = "公司总机";
private final String string11 = "ISDN";
private final String string12 = "总机";
private final String string13 = "其他传真";
private final String string14 = "无线装置";
private final String string15 = "电报";
private final String string16 = "TTY TDD";
private final String string17 = "单位手机";
private final String string18 = "单位寻呼机";
private final String string19 = "助理";
private final String string20 = "彩信";
private final String string21 = "自定义";
public void initStringListOfNumber()//初始化listOfPhoneString
{
listOfPhoneString.add(string1);
listOfPhoneString.add(string2);
listOfPhoneString.add(string3);
listOfPhoneString.add(string4);
listOfPhoneString.add(string5);
listOfPhoneString.add(string6);
listOfPhoneString.add(string7);
listOfPhoneString.add(string8);
listOfPhoneString.add(string9);
listOfPhoneString.add(string10);
listOfPhoneString.add(string11);
listOfPhoneString.add(string12);
listOfPhoneString.add(string13);
listOfPhoneString.add(string14);
listOfPhoneString.add(string15);
listOfPhoneString.add(string16);
listOfPhoneString.add(string17);
listOfPhoneString.add(string18);
listOfPhoneString.add(string19);
listOfPhoneString.add(string20);
listOfPhoneString.add(string21);
}
3.开启线程到系统中获取数据
Runnable r = new Runnable()
{
//处理数据
public void run()
{
listOfResolvedPhone = QueryLinkmanInfoByRawContactId.queryPhoneInfo(EditLinkman.this, rawContactId);
Message msg = new Message();
myHandler.sendMessage(msg);//得到电话信息以后通知更新界面
}
};
在queryPhoneInfo()中,我们调用并查询了系统的信息,并对系统中的数据进行匹配处理,最终返回回来得到listOfResolvedPhone会是List<HashMap<String, String>>保存的用户的信息,比如:PHONE_TYPE_ID:1
PHONE_TYPE:住宅 PHONE_NUMBER:13023895363
4.更新界面
private Handler myHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
showPhone();//根据listOfResolvedPhone中的值初始化显示电话信息的LinearLayout->llPhone
}
};
public void showPhone()
{
//让顶部啊按钮监听事件
btnPhone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 增加一个子View
System.out.println("增加一个Phone子View");
addSubViewOfPhone();
}
});
if(listOfResolvedPhone.isEmpty())
{
System.out.println("PhoneList is empty");
//do nothing...
}
else
{
System.out.println("PhoneList is NO empty");
int sizeOfResolvedPhone = listOfResolvedPhone.size();
for(int i = 0; i < sizeOfResolvedPhone; i++)
{
// String phoneTypeId = listOfResolvedPhone.get(i).get(LD_PHONE_ID);//这样做虽然比较好,但是逻辑太乱了,暂时不考虑
String phoneType = listOfResolvedPhone.get(i).get(LD_PHONE_TYPE);
String phoneNumber = listOfResolvedPhone.get(i).get(LD_PHONE);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View subView = inflater.inflate(R.layout.sub_view_of_add_linkman, null);
Button btnChooseType = (Button)subView.findViewById(R.id.svoal_btn_choose_type);
EditText rlEditText = (EditText)subView.findViewById(R.id.svoal_et_input);
rlEditText.setInputType(InputType.TYPE_CLASS_NUMBER);//设置输入类型和键盘为数字
RelativeLayout rlDel = (RelativeLayout)subView.findViewById(R.id.svoal_rl_del);
System.out.println("phoneType:" + phoneType);
System.out.println("phoneNumber:" + phoneNumber);
btnChooseType.setText(phoneType);
rlEditText.setText(phoneNumber);
btnChooseType.setOnClickListener(new BtnChooseNumberTypeClickedListener());
rlDel.setOnClickListener(new RlNumberDelClickedListener());
subView.setTag(tagFlag);
btnChooseType.setTag(tagFlag);
rlEditText.setTag(tagFlag);
rlDel.setTag(tagFlag);
listOfPhoneSubView.add(subView);
listOfPhoneButton.add(btnChooseType);
listOfPhoneEditText.add(rlEditText);
listOfPhoneTag.add(rlDel.getTag());
tagFlag ++;
llPhone.addView(subView, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);//增加一个View
}
}
}
tag的作用是什么呢?
为什么要给每一个控件都加上一个Tag呢?
有两个方面:
1.当用户删除某一个元素的时候,不一定是从哪里删除,有可能是从列表中间删除,所以当用户点击子View上面的删除按钮的时候,需要通过v.getTag()获得要被删除的是哪个元素。通过这个Tag就可以将他们都删除了。
2.当点击选择电话类型的时候,同样,你需要把用户选择的哪个类型的字符串返回回来,你如何锁定是哪个Button需要被更改呢?同样也是通过Tag,我们在打开选择类型的Activity的时候,不仅将让用户进行选择的List传递过去,也将Tag传递过去,传递过去以后什么都不做,直接返回,返回回来以后就可以找到是哪个Button上面的类型名需要更改了。
public void addSubViewOfPhone()
{
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View subView = inflater.inflate(R.layout.sub_view_of_add_linkman, null);
Button btnChooseType = (Button)subView.findViewById(R.id.svoal_btn_choose_type);//选择号码类型
EditText rlEditText = (EditText)subView.findViewById(R.id.svoal_et_input);//接收号码的输入
rlEditText.setHint("电话");
rlEditText.setInputType(InputType.TYPE_CLASS_NUMBER);//设置输入类型和键盘为数字
RelativeLayout rlDel = (RelativeLayout)subView.findViewById(R.id.svoal_rl_del);//右边的删除按钮
btnChooseType.setOnClickListener(new BtnChoosePhoneTypeClickedListener());
rlDel.setOnClickListener(new RlNumberDelClickedListener());
//在创建的时候判断下现在是第几个了,设置btnChooseType上默认显示的内容
switch(listOfPhoneTag.size())
{
case 0:
btnChooseType.setText(string1);
break;
case 1:
btnChooseType.setText(string2);
break;
case 2:
btnChooseType.setText(string3);
break;
case 3:
btnChooseType.setText(string4);
break;
case 4:
btnChooseType.setText(string5);
break;
case 5:
btnChooseType.setText(string6);
break;
case 6:
btnChooseType.setText(string7);
break;
case 7:
btnChooseType.setText(string8);
break;
default:
btnChooseType.setText(string8);//其他的暂时不考虑啦。就取前8吧
}
subView.setTag(tagFlagOfPhone);
btnChooseType.setTag(tagFlagOfPhone);
rlEditText.setTag(tagFlagOfPhone);
rlDel.setTag(tagFlagOfPhone);
listOfPhoneSubView.add(subView);
listOfPhoneButton.add(btnChooseType);
listOfPhoneEditText.add(rlEditText);
listOfPhoneTag.add(rlDel.getTag());
tagFlagOfPhone ++;
llPhone.addView(subView, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
/**
* 删除一个电话subView
* */
class RlNumberDelClickedListener implements OnClickListener
{
public void onClick(View v)
{
System.out.println("删除:");
//根据tag找到它在list中的位置,删除中所有list中对应的
int id = -1;
int sizeOfTag = listOfPhoneTag.size();
for(int i = 0; i < sizeOfTag; i++)
{
if((Integer)listOfPhoneTag.get(i) == (Integer)(v.getTag()))
{
System.out.println("找到并删除了索引:" + i);
id = i;
llPhone.removeView(listOfPhoneSubView.get(id));//越界了
listOfPhoneSubView.remove(id);
listOfPhoneButton.remove(id);
listOfPhoneEditText.remove(id);
listOfPhoneTag.remove(id);//记得Remove掉自己啊!!!
break;
}
else
{
System.out.println("不可能找不到的!");
}
}
}
}
/**
* 选择号码类型的按钮被点击的时候
* */
private int tagFlagOfPhone = 0;
class BtnChoosePhoneTypeClickedListener implements OnClickListener
{
public void onClick(View v)
{
System.out.println("BtnChooseTypeClickedListener:");
System.out.println("v.getTag():" + (Integer)v.getTag());
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putInt(TAG, (Integer)v.getTag());//这个传递到DialogActivity再从那边传递过来,就可以知道是谁传递的了
bundle.putString(TITLE, "请选择号码类型");
bundle.putString(FLAG, NUMBER);
if(listOfPhoneString.isEmpty())
{
initStringListOfNumber();
bundle.putStringArrayList(STRING_LIST, (ArrayList<String>)listOfPhoneString);
}
else
{
bundle.putStringArrayList(STRING_LIST, (ArrayList<String>)listOfPhoneString);
}
intent.putExtras(bundle);
intent.setClass(EditLinkman.this, DialogActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
}
/**
* 将TAG的值传递进来,到TAG数组中进行值匹配
* 将匹配到的索引返回
* */
public int getIndexOfPhoneTag(int TAG)
{
int index = -1;
int sizeOfTag = listOfPhoneTag.size();
for(int i = 0; i < sizeOfTag; i++)
{
if((Integer)listOfPhoneTag.get(i) == TAG)
{
index = i;
}
}
return index;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
System.out.println("EditLinkman--onActivityResult");
if(requestCode == REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
System.out.println("EditLinkman--RESULT_OK");
Bundle extras = data.getExtras();
if (extras != null)
{
System.out.println("extras != null:FLAG:" + extras.getString(FLAG).toString());
if(extras.getInt(TAG) == -1)
{
System.out.println("收到传递回来的空TAG" + extras.getString("TAG"));
}
else if(extras.getString(FLAG).equals(NUMBER))
{
System.out.println("收到传递回来的值" + extras.getInt(TAG));
listOfPhoneButton.get(getIndexOfPhoneTag(extras.getInt(TAG))).setText(extras.getString(VALUE));
}
}
}
}
}