我正在查询android Contact类,并且能够获取联系人姓名和号码,并能够在线性布局中添加多个文本视图,其中包含联系人的号码和名称,并且能够在共享首选项中保存单个textview数值并将其重新检索,如何我可以动态添加textviews值的数组(电话号码(字符串)在共享首选项中。如果任何人提供示例代码,它将帮助我很多,因为我第一次这样做,通过我搜索了很多关于这个话题但没有运气。
这是我的代码:
public class Cont extends Fragment {
protected static final int PICK_CONTACT = 0;
Button showConts;
TextView[] resultTextView;
ViewGroup addItems;
final int N = 1;
String name;
String cNumber;
String[] array;
String nameContact;
String number;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View contViews = inflater.inflate(R.layout.cont, container, false);
addItems = (LinearLayout) contViews.findViewById(R.id.adds);
showConts = (Button) contViews.findViewById(R.id.opens);
resultTextView = new TextView[N]; //create an empty array;
// create a new textview
for (int i = 0; i < N; i++) {
final TextView rowTextView = new TextView(getActivity());
resultTextView = new TextView[N]; // create an empty array;
addItems.addView(rowTextView);
resultTextView[i] = rowTextView;
if(resultTextView[i] != null ){
resultTextView[i].setText(nameContact+ " "+ cNumber);
}
}
showConts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
return contViews;
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode, resultCode, data);
switch(reqCode)
{
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = getActivity().managedQuery(contactData, null, null, null, null);
if (c.moveToFirst())
{
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
{
Cursor phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Toast.makeText(getActivity(), cNumber, Toast.LENGTH_SHORT).show();
nameContact = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
for (int i = 0; i < N; i++) {
final TextView rowTextView = new TextView(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
params.setMargins(0,0,0,10);
resultTextView = new TextView[N];
rowTextView.setTextSize(34);
rowTextView.setHeight(140);
rowTextView.setGravity(Gravity.CENTER);
rowTextView.setBackgroundColor(Color.parseColor("#7d3f98"));
rowTextView.setTextColor(Color.WHITE);
//rowTextView.setBackgroundResource(R.drawable.corn);
addItems.addView(rowTextView);
resultTextView[i] = rowTextView;
resultTextView[i].setLayoutParams(params);
if(resultTextView[i] != null ){
resultTextView[i].setText(nameContact+ " "+ cNumber);
}
resultTextView[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String res1 = rowTextView.getText().toString();
System.out.println("num " + res1.length());
res1 = res1.split(" ")[res1.split(" ").length - 1];
startDialActivity(res1);
}
private void startDialActivity(String result) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+result));
startActivity(intent);
}
});
}
}
}
}}}}
答案 0 :(得分:0)
尝试使用google PreferenceActivity,您不必使用传统方式(SharedPreference sp = new ....) ,但我认为SharedPreferenceActivity非常简单,它是原生的,非常简单和强大的!
答案 1 :(得分:0)
您可以制作JSONArray并通过制作JSONObject添加名称和电话号码,并将这些JSONObject添加到JSONArray。
JSONObject mainObject=null;
try{
mainObject=new JSONObject();
JSONArray arr = new JSONArray();
for (int i = 0; i < 10; i++) {
JSONObject objName = new JSONObject();
JSONObject objPhone = new JSONObject();
objName.put("name", new String("abbcc"));
objPhone.put("phone", new String("dfdfgdf"));
arr.put(i,objName);
arr.put(i,objPhone);
}
mainObject.put("mainObj",arr);
System.out.println(mainObject.toString());
}
catch(Exception e){
}
shared.edit().putString("details",mainObject.toString()).commit();
然后从sharedPreferences中获取所需的字符串以及从JSONObject和JSONArray获取详细信息