2014-11-16 142 views
0

在此先感謝您的幫助,我的代碼在文本文件中顯示並顯示在ListView中,我在文本字段中的一行中包含Name和youtube。將字符串轉換爲ArrayList

但我看着試圖做的是獲取文本文件中的youtube字符串,並將其傳遞給我的新Activity類作爲webview播放視頻 只是想知道如何做到這一點,我怎麼能通過這個字符串到我的模型類中的Setters爲了得到它的一個實例,我需要將字符串轉換爲ArrayListString嗎?

public class menuFragment extends ListFragment { 
    ArrayList<model> songList = new ArrayList<model>(); 
    public String[] listSongs = new String[]{}; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.list_fragment, container, false); 
     loadSongs(); 
     return view; 
    } 
    public void loadSongs() { 
     try { 
      Resources ResFiles = getResources(); 
      InputStream ReadDbFile = ResFiles.openRawResource(R.raw.songs); 
      byte[] Bytes = new byte[ReadDbFile.available()]; 
      ReadDbFile.read(Bytes); 
      String DbLines = new String(Bytes); 
      listSongs = DbLines.split(","); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
        android.R.layout.simple_list_item_1, listSongs); 
      setListAdapter(adapter); 
     } catch (Exception e) { 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     Intent i = new Intent(getActivity(), playVid.class); 
     model selectedSong = MainController.getInstance().getSongs().get(position); 
     i.putExtra("selectedSong", selectedSong); 
     startActivity(i); 
    } 


public class model implements Serializable { 
    private String name; 
    private String url; 

    public model(String name, String url) { 
     this.name=name; 
     this.url = url; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getUrl(){ 
     return url; 
    } 
    public void setUrl(String url){ 
     this.url = url; 
    } 

public class MainController 
{ 
    private static MainController instance; 
    private ArrayList<model> songList; 
    private MainController() 
    { 
     this.songList = new ArrayList<model>(); 
    } 
    public static MainController getInstance() 
    { 
     if(instance == null) 
     { 
      instance = new MainController(); 
     } 
     return instance; 
    } 
    public void addFlight(String name, String singer, String url) 
    { 
     model f = new model(name,singer,url); 
     this.songList.add(f); 
    } 
    public ArrayList<model> getSongs() 
    { 
     return this.songList; 
    } 
+0

實例? – Simas

+0

EdSheeran#http://www.youtube.com/watch?v = lp-EO5I60KA, – David

回答

0

建議類名應該以大寫 - 型號開頭。

你只有三個變量傳遞給另一個活動,所以你可以有三個putExtra,你的意圖不需要ArrayList,Sir。

Model selectedSong = MainController.getInstance().getSongs().get(position); 
    i.putExtra("name", selectedSong.getName()); 
    i.putExtra("singer", selectedSong.getSinger()); 
    i.putExtra("url", selectedSong.getUrl()); 
    startActivity(i); 

而內的另一個活動的onCreate,我們可以像這樣訪問方式這三個值,你的字符串的

Intent mIntent = getIntent(); 
    String name = mIntent.getStringExtra("name"); 
    String singer = mIntent.getStringExtra("singer"); 
    String url = mIntent.getStringExtra("url"); 
+0

謝謝你的幫助..當我運行代碼時,我得到了java.lang.IndexOutOfBoundsException:無效的索引3,大小是0.它指向模型selectedSong = MainController.getInstance.getSongs()。get(position)它似乎這可能是一個錯誤,因爲我不指出我的ArrayAdapter模型類? – David

+0

這是因爲,在getSongs方法獲取它們之前,您需要使用addFlight方法設置它們。我沒有看到你的代碼。 – Chitrang

+0

好的,謝謝,我沒有注意到,缺少,我應該將addSong放入一個無效的方法或有需要嗎? – David