在android中一个接一个地调用4个方法
Posted
技术标签:
【中文标题】在android中一个接一个地调用4个方法【英文标题】:Calling 4 methods one after the othe in android 【发布时间】:2015-04-24 04:29:23 【问题描述】:我想知道如何在android中一个接一个地调用4个menthods。例如:
从getSim()方法开始
getSim()方法完成后启动方法getPhone()
方法getPhone()当完成start方法compare()
方法 compare() 完成时 start 方法 display()
private void getSim()
System.out.println("In get Sim");
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = context.getContentResolver().query(simUri,null,null,null,null);
Log.i("PhoneContact", "total: "+cursorSim.getCount());
if(cursorSim.getCount()>0)
while (cursorSim.moveToNext())
ClsSimPhonename =cursorSim.getString(cursorSim.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim.getColumnIndex("number"));
String xyz= ClsSimphoneNo.replaceAll("\\D","").replaceAll("&", "").replaceAll("\\(","").replaceAll("\\)","").replaceAll("\\s", "").replaceAll("-", "");
Log.i("PhoneContact", "name: "+ClsSimPhonename+" phone: "+ClsSimphoneNo);
// getSimPhone.add(ClsSimPhonename+" sim");
getSimPhone.add(ClsSimPhonename);
mb.add(xyz);
cursorSim.close();
`
`private void getPhone()
cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.getCount()>0)
while (cursor.moveToNext())
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]id, null);
while (pCur.moveToNext())
phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mbnum =phone.replaceAll("\\(","").replaceAll("\\)","").replaceAll("\\s", "").replaceAll("-", "");
mb.add(mbnum);
pCur.close();
getSimPhone.add(name+" ph");
cursor.close();
`
` private void compare()
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereNotEqualTo("objectId", currentUserId);
query.findInBackground(new FindCallback<ParseUser>()
public void done(List<ParseUser> userList, com.parse.ParseException e)
if (e == null)
for (int i=0; i<userList.size(); i++)
names.add(userList.get(i).getUsername().toString());
ph.add(userList.get(i).getString("mobile"));
for(int z=0;z<mb.size();z++)
for (int i=0; i<ph.size(); i++)
if(mb.get(z).equals(ph.get(i)))
Toast.makeText(ShowContactsWithApp.this,"z mb"+mb.get(z)+ "\n i ph"+ph.get(i) + "\n z name" +getSimPhone.get(z)+"\n i name"+getSimPhone.get(i),Toast.LENGTH_LONG).show();
values.add(getSimPhone.get(z));
Toast.makeText(ShowContactsWithApp.this,"got a record",Toast.LENGTH_SHORT).show();
Toast.makeText(ShowContactsWithApp.this,"values count :"+values.size(),Toast.LENGTH_SHORT).show();
else
Toast.makeText(ShowContactsWithApp.this,"Error loading user list",Toast.LENGTH_SHORT).show();
);
return values;
`
`display ()
adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1, values);
lst.setAdapter(adapter);`
首先我得到 sim 联系人,第二个我得到电话联系人,第三个我与解析存储的联系人进行比较,第四个我显示比较。
【问题讨论】:
提供你想要达到的代码 这是代码的正常行为(除非您正在执行线程或异步代码)。只需按顺序调用它们 A();乙(); C(); D(); 当我发布我的代码时出现格式错误 你能给我一个示例代码吗 把你的代码放在反勾号之间`像这样` 【参考方案1】:你只需这样做:
myMethod1();
myMethod2();
myMethod3();
myMethod4();
【讨论】:
【参考方案2】:试试这样-
A()
//here do your coding. at the end call next method
//here you can call your method B
B();
B()
//here do your coding. at the end call next method
//here you can call your method C
C();
C()
//here do your coding. at the end call next method
//here you can call your method D
D();
D()
//here do your coding.
【讨论】:
我试过了,但是在调用 B() 之前执行 A() 的所有语句之前,调用了 B() 。等等。 @AnaghaMagar 你能告诉我你的方法 A、B、C、D 是哪一种吗? @AnaghaMagar 如果可能,然后使用 try catch finally 语句。我不确定 getSim();, getPhone();, compare();和显示();是四种方法吗? 设置 i=1;在您的 while 循环中,增加 i 的值并检查天气 i==cursorSim.getCount()。如果两者相等,则您的 while 循环退出。所以在那个 if 循环中,调用下一个方法。以上是关于在android中一个接一个地调用4个方法的主要内容,如果未能解决你的问题,请参考以下文章