2011-12-15 7 views
1

私はプロジェクト用のアプリを持っています。私の考えは、3タブアプリを作成することでした、各タブには、トピックを表すいくつかのボタンがあります。各トピックには、特定のボタンをクリックすると開いたアクティビティに表示したい情報が入ったtxtファイルがあります。同じアクティビティが開かれますが、入力されたテキストは押されたボタンによって異なります。/rawに保存されたテキストでアクティビティを入力する方法

現在、テキストを表示する機能以外はすべてがあります。現在、/ rawにはtxtとして保存されています。

私は入力リーダーとtxtとxmlを見ています(以前は簡単でした)、私はまだ苦労しています。

特定のtxtファイルを表示することができても、どのボタンが押されたかに応じて動作させる方法がわかりません。

コンテンツ保持アクティビティには、.txtファイルのテキストを入力する予定のテキストビューがあります。

ご協力いただければ幸いです。

このキャッチで構文エラーが発生しました。

public class MartialTab extends Activity { 
Button btn1, btn2; 
String choice; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    TextView textview = new TextView(this); 
    textview.setText("This is the martial arts tab"); 
    setContentView(textview); 

    setContentView(R.layout.martial_tab);//takes layout from martial_tab.xml 

    btn1 = (Button) findViewById(R.id.kickboxingButton);//instantiates a button called btn1 one from the xml 
    btn1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      kickBoxingBut(v, choice); 
     }//calls the method 
    });//end of method 

    btn2 = (Button) findViewById(R.id.grecoroman);//instantiates a button called btn1 one from the xml 
    btn2.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      grecoromanBut(v); 
     }//calls the method 
    });//end of method 


} 



    // private void kickBoxingBut() {//button1 method 
    //new AlertDialog.Builder(this).setTitle("AlertNotification").setMessage(
     // "This is an Alert Dialogue Toast").setNeutralButton("Click Me!", null).show(); 
    //creates an alert notification with the above text 
    //} 
    public String kickBoxingBut(View view, String s) { 
     startActivity(new Intent(this, ContentHolder.class)); 
     this.choice = "kick"; 

     return choice; 

    } 

    public void grecoromanBut(View view) { 
     startActivity(new Intent(this, ContentHolder.class)); 
    }  
} 

その中にボタン付きEDIT

活動を、それを理解しようと最後の15分を費やした情報を保持している活動

 public class ContentHolder extends Activity { 



    public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    Context m_Context = getApplicationContext(); 

    TextView textview = new TextView(this); 
    textview.setText("This is the content view"); 
    setContentView(textview); 

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null) { 
    String value = extras.getString("choice"); 
    } 



    InputStream input; 

    try { 
     input = m_Context.getAssets().open("choice"); 

    DataInputStream in = new DataInputStream(input); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    // Read File Line By Line 
    while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     System.out.println(strLine); 
     textview.setText(strLine); 
    } 
    // Close the input stream 
    in.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
    //setContentView(R.layout.main); 

    } 

このsetContentView問題です。上記のように、戻り値の型を期待しているので動作しません。それをContentHolderに変更しても動作しません。

+0

あなたが見ることができるように、私はそれはそれでは動作しないだろう、一番下にsetcontentViewをコメントアウトそこ。私は完全にatmに固執しています。 – TroothHertz

+0

基本的には、ボタンを押したときに応じてテキスト文書を読み込もうとしていますか? –

+0

ユーザがボタンをクリックすると、その中のテキストを含むアクティビティが開きます。それは簡単だろうと思った。また、その印刷ステートメントが気に入らない – TroothHertz

答えて

0

あなたは資産のtxtファイルを開いて、このような行でラインを読むことができます:

InputStream input = context.getAssets().open(fileName); 
DataInputStream in = new DataInputStream(input); 
BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
String strLine; 
//Read File Line By Line 
while ((strLine = br.readLine()) != null) { 
    // Print the content on the console 
    System.out.println (strLine); 
} 
//Close the input stream 
in.close(); 
} catch (Exception e){//Catch exception if any 
    System.err.println("Error: " + e.getMessage()); 
} 

これはあなたのために良いスタートです、あなたはTextViewで読むの行を表示します。一度これを行うと、次のステップに進むことができます。

EDIT

package com.example.helloandroid; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class HelloAndroid extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // This is how you get the context 
     Context m_Context = getApplicationContext(); 

     // This is how you show text programatically, there is also xml option, which I recommend instead. 
     TextView tv = new TextView(this); 
     tv.setText("Hello, Android"); 
     setContentView(tv); 
} 

http://developer.android.com/resources/tutorials/hello-world.html

EDIT2

public class ContentHolder extends Activity { 

    String fileName; 

    public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    Context m_Context = getApplicationContext(); 

    TextView textview = new TextView(this); 
    textview.setText("This is the content view"); 

    InputStream input; 
    try { 
     input = m_Context.getAssets().open(fileName); 

    DataInputStream in = new DataInputStream(input); 
    BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
    String strLine; 
    // Read File Line By Line 
    while ((strLine = br.readLine()) != null) { 
     // Print the content on the console 
     System.out.println(strLine); 
     textview.setText(strLine); 
    } 
    // Close the input stream 
    in.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    setContentView(R.layout.main); 
} 
+0

それはちょうどEclipseコンソールにそれを印刷しませんか?また、私が掘ったいくつかのサンプルで使用されている文脈を見てきましたが、私はそれをどうするか分かりません。 eclipseはそれを解決することはできませんし、Googleは友好的ではありません – TroothHertz

+0

はい、私はあなたにアイデアを与えるためにコードを投稿します。 – Caner

+0

Androidでは、プログラムの開始点は 'Activity'を拡張するクラスです。そのクラスでは 'getApplicationContext'を呼び出すことができます。 – Caner

関連する問題