2013-10-31 50 views
0

我已聲明「Pair(List(String)), (List(String))」以接收來自方法的返回數據。然而,因爲它錯誤說「The method second() is undefined for the type Pair」我無法檢索兩個單獨的列表方法second()未定義類型對

這是我的代碼:

public class EditPeople_ListFragment extends ListFragment { 

public Pair<List<String>, List<String>> ReadPeopleFile(FileReader peopleReader){ 
    ///Soon parsed into a string list, by delimiting and separating into two string arrays, 
    char characterList[] = new char[10000]; // I'm assuming that 1000 is enough here 
    int numChars = 0; 
    try { 
     numChars = peopleReader.read(characterList); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    String parseString = new String(characterList, 0, numChars);///Creates a string parseString. 
    List<String> parseString_array = new ArrayList<String>();///Initialises a list of strings 
    parseString_array = Arrays.asList(parseString.split(";"));///Splits the parseString into the parseString_array 
    List <String> mobileNumber = new ArrayList<String>();///Initialises a list of strings, meant to be mobile numbers. 
    List <String> name = new ArrayList<String>();///Initialises a list of string, meant to be names 
    String[] parts = new String[2]; 
    for (String peopleDetails : parseString_array){///sets up a loop for each of the strings in parseString_array, to be split into mobile numbers, and name. 
     ///delimiter: ";" is for each peopleDetail string. Delimiter: "," separates mobile number and names in each peopleDetails string. 
     parts = peopleDetails.split(","); 
     mobileNumber.add(parts[0]); 
     name.add(parts[1]);   


    } 
    try { 
     peopleReader.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    return new Pair(mobileNumber, name); 
    ///Then use on return value nameList = ------.getname(); 
    ///and use --------.getmobileNumber(); 








} 



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    FileReader peopleFile_Reader = new FileReader("PartyApp_PeopleFile"); 
    Pair returnedpair = ReadPeopleFile(peopleFile_Reader); 
    List<String> mobileList = returnedpair.first(); 
    List<String> nameList = returnedpair.second(); 





    ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), R.id.ListView_EditPeopleFragment, People_List); 
    setListAdapter(adapter); 
    return super.onCreateView(inflater, container, savedInstanceState); 
} 
} 

感謝您的幫助!

回答

0

只需使用:

Pair<List<String>, List<String>> returnedpair = ReadPeopleFile(peopleFile_Reader);  
List<String> mobileList = returnedpair.first; 
List<String> nameList = returnedpair.second; 

firstsecond是公共領域,有這樣的名字沒有方法。

+0

好吧,這似乎工作性質,雖然要增加對mobileList和名稱列表鑄造,從Object強制列出。這應該發生嗎? –

+0

查看更新的答案。你可以像我已經完成的那樣聲明'returnedpair',或者你可以從'Object'強制輸入'first'和'second'來列表 –

+0

謝謝,現在正在工作 –

0

firstsecond都爲Pair類沒有方法

+0

好吧,這似乎工作,儘管我必須爲mobileList添加一個強制轉換和nameList,從Object轉換爲列表。這應該發生嗎? –

相關問題