2011-09-27 204 views
0

我得到了這段代碼,我一直在努力,它將會是一個遊戲,我試圖創建只是在兩個玩家之間切換。每次它切換它應該寫出一個問題,如真相或敢。不允許兩次寫同一個問題,因此應該能夠看到它是否已經使用過這個問題。Android/Java:導入文本文件並從文本文件中讀出隨機行

到目前爲止,我已經運行了它,並且每次點擊下一步時它都會在兩個玩家之間切換。

但是我有很多的麻煩,從所謂的男人txt文件中獲取數據,這裏有三條線,文本1文本2和文字3。它應該能夠隨機取出並知道它是否已經讀過。我不能讓當前的InputStream工作,它說man文件是int,但它包含字符串?

這裏是到目前爲止的代碼:使用Resources.openRawResource

package truthordare; 

import java.io.FileInputStream; 
import java.io.IOException; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
//import android.content.Intent; 


public class truthordareActivity extends Activity 

{ 
    public int i = 0; 
    //public String man; 
    //public String woman; 
    TextView w; 
    TextView m; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     { 
     final Button buttonPlay = (Button) findViewById(R.id.buttonPlay); 
     buttonPlay.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
       gameCode(); 
       } 
     }); 
    } 
} 


    /*public FileInputStream openFileInput(int man) throws IOException 
{ 
    String collected = null; 
    try 
    { 
     FileInputStream fis = openFileInput(R.raw.man); 
     byte[] dataArray = new byte[fis.available()]; 
     while (fis.read(dataArray) != -1) 
      { 
      collected = new String(dataArray); 
      } 
     fis.close(); 
    } 
    catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 
    return null; 

} 
*/ 



public void gameCode() 
    { 
     // Perform action on click 
     setContentView(R.layout.game); 
     if(i == 0) 
      { 
       w = (TextView)findViewById(R.id.textView1); 
       w.setText("This is a player1"); 
       i = 1; 
       final Button buttonNext = (Button) findViewById(R.id.buttonNext); 
       buttonNext.setOnClickListener(new View.OnClickListener() 
       { 
        public void onClick(View v) 
        { 
         gameCode(); 
        } 
       }); 
      } 
     else 
      { 
       m = (TextView)findViewById(R.id.textView1); 
       m.setText("This is player2"); 
       i = 0; 
       final Button buttonNext = (Button) findViewById(R.id.buttonNext); 
       buttonNext.setOnClickListener(new View.OnClickListener() 
       { 
        public void onClick(View v) 
        { 
         gameCode(); 
        } 
       }); 
      } 

    } 

} 

回答

1

您是否考慮過使用XML文件來解決您的問題?從您提供的信息,結構應該是這樣的:

<questions> 
    <question> 
     <id>1</id> 
     <text>This is question nr. 1</text> 
    </question> 
    <question> 
     <id>2</id> 
     <text>This is question nr. 2</text> 
    </question> 
    <question> 
     <id>3</id> 
     <text>This is question nr. 3</text> 
    </question> 
</questions> 

加載所有的問題到列表/ ArrayList的問題爲對象,當提出問題 - 從列表中刪除。如果您需要稍後保存問題,請不要將其刪除,而應將所有問題的ID保存在另一個列表中,並且當您嘗試獲取下一個問題時,請確保其ID不在ID列表中。

爲了讓你只使用一個隨機數生成器,爲您提供0和則爲list.size()之間的值的隨機問題。

這樣你就不必花時間打開和關閉InputStreams所有的時間,你必須確保一個問題是隻問了一次,一個簡單的方法。

+0

我已經設法在xml文件中創建一個字符串數組並讀取並顯示它。我將如何製作該功能,以便它不會採用兩次相同的問題? – psalomonsen

+0

這是我現在得到的samle代碼(我做了一個測試項目,當我得到它的時候我會實現它)只需點擊鏈接。 [鏈接](http://pastebin.com/JWhK8cuZ) – psalomonsen

+0

您應該將問題加載到一個List/ArrayList而不是一個數組中。這樣你可以在閱讀完問題後使用列表中的'remove()'方法,並且你不會得到兩次相同的問題。 – kaspermoerch

1

InputStream。在你的情況下,這是getResources().openRawResource(R.raw.man)