无法从其他类方法中检索值

时间:2014-09-20 09:34:10

标签: android

我有一个人。 A类方法

 public String getContactNameFromNumber(String number) {
        // define the columns I want the query to return

        String[] projection = new String[]{
                Phones.DISPLAY_NAME,
                Phones.NUMBER};

        // encode the phone number and build the filter URI
        Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(fname));

        // query time
        Cursor c = getContentResolver().query(contactUri, projection, null,
                null, null);

        // if the query returns 1 or more results
        // return the first result
        if (c.moveToFirst()) {
            String name = c.getString(c
                    .getColumnIndex(Phones.DISPLAY_NAME));
            return name;
        }

        // return the original number if no match was found
        return number;


        }

我想从变量fname中检索值,并在其他B类的listview中插入值

3 个答案:

答案 0 :(得分:0)

您可以在任何公共静态变量中获取此方法的值 然后在B类中,您可以编写ClassA.name_of_static_variable;

答案 1 :(得分:0)

假设您有两个ClassA和ClassB类。 ClassA中返回值的任何方法,以便您可以在公共静态变量中获取该值。 public static String _varName = null; //让你在这个变量中返回值。

在ClassB中你可以写出ClassA._varName的名字; 那么你可以在ClassB中获得ClassA的值。

答案 2 :(得分:0)

静态引用作为类之间的桥接通信的示例

//类

public class ClassA {
    public static String fName = "";

    public void getContactNameFromNumber(String number) {
        //calucate fName value
    }
}

//类

public class ClassB {
    public static String fName = "";

    private void fromSomeMethod() {
        String fName = ClassA.fName;
    }

}