2011-01-15 17 views
0

以下は主なアクティビティのコードです。 addJoke()メソッドでTextViewを追加しましたが、LinearLayoutには表示されません。解決してください。TextVIewがLinearLayoutに表示されない、またはLinearLayoutに追加されない

package edu.calpoly.android.lab2; 
import java.util.ArrayList; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnKeyListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class AdvancedJokeList extends Activity { 
    private static int i=0; 
    //protected String m_strAuthorName; 
    protected ArrayList<Joke> m_arrJokeList=new ArrayList<Joke>(); 
    //protected JokeListAdapter m_jokeAdapter; 

    /** 
    * ViewGroup used for maintaining a list of Views that each display Jokes. 
    **/ 
    protected LinearLayout m_vwJokeLayout; 
    protected EditText m_vwJokeEditText; 
    protected Button m_vwJokeButton; 
    protected int m_nDarkColor; 
    protected int m_nLightColor; 
    /** 
    * Filter Options Submenu constants 
    */ 
    protected static final int FILTER_OPTIONS = 1; 
    protected static final int LIKE = Menu.FIRST + 1; 
    protected static final int DISLIKE = Menu.FIRST + 2; 
    protected static final int UNRATED = Menu.FIRST + 3; 
    protected static final int SHOW_ALL = Menu.FIRST + 4; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     initLayout(); 
     String[] jokestring=getResources().getStringArray(R.array.jokeList);  
     for(String str : jokestring) 
     { 
       Joke j=new Joke(); 
       j.setJoke(str); 
       addJoke(j); 
     } 
     initAddJokeListeners(); 
    } 

    protected void addJokeImplementation(){ 
     String strJoke=m_vwJokeEditText.getText().toString().trim(); 
     if(!strJoke.equals("")) 
     { 
     Joke joke=new Joke(); 
     joke.setJoke(strJoke); 
     addJoke(joke); 
     } 
    } 

    protected void initLayout() { 
     setContentView(R.layout.advanced); 
     m_vwJokeLayout=(LinearLayout)findViewById(R.id.jokeListViewGroup); 
     m_vwJokeEditText=(EditText)findViewById(R.id.newJokeEditText); 
     m_vwJokeButton=(Button)findViewById(R.id.addJokeButton); 
    } 

    protected void initAddJokeListeners() { 
     // TODO 
     m_vwJokeEditText.setOnKeyListener(new OnKeyListener(){ 
      @Override 
      public boolean onKey(View v,int keyCOde, KeyEvent event){ 
       if(event.getAction()==KeyEvent.ACTION_DOWN) 
       { 
        if(keyCOde==KeyEvent.KEYCODE_DPAD_CENTER) 
        { 
         addJokeImplementation(); 
        } 
       } 
       return false; 
      } 

     }); 
     m_vwJokeButton.setOnClickListener(new OnClickListener() { 
      @Override 
     public void onClick(View view) { 
     //Implement code to add a new joke here... 
       addJokeImplementation(); 
      } 
     }); 
    } 

    protected void addJoke(Joke joke) { 
     if(!m_arrJokeList.contains(joke)) 
      { 
      m_arrJokeList.add(joke); 
      //I also added textview here this one also 
      //doesn't appear in Emulator layout, wondering whats wrong?; 
      TextView TV=new TextView(this); 
      TV.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT)); 
      TV.setText(joke.toString()); 
      m_vwJokeLayout.addView(TV); 
      m_nDarkColor=getResources().getColor(R.color.dark); 
      m_nLightColor=getResources().getColor(R.color.light);  
      if(i==0) 
      { 
       TV.setBackgroundColor(m_nLightColor); 
       i=1; 
      } 
      else 
      { 
       TV.setBackgroundColor(m_nDarkColor); 
       i=0; 
      } 
      m_vwJokeEditText.setText(""); 
      InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(m_vwJokeEditText.getWindowToken(), 0); 
      } 
    }   

    /** 
    * Method used to retrieve Jokes from online server. The getJoke script 
    * takes a single optional parameter, which should be encode in "UTF-8". 
    * This parameter allows tells script to only retrieve Jokes whose author 
    * name matches the value in the parameter. 
    * 
    * param-1) "author": The author of the joke. 
    * 
    * URL: http://simexusa.com/aac/getJokes.php? 
    * 
    */ 
    protected void getJokesFromServer() { 
     // TODO 
    } 

    /** 
    * This method uploads a single Joke to the server. This method should test 
    * the response from the server and display success or failure to the user 
    * via a Toast Notification 
    * 
    * The addJoke script on the server requires two parameters, both of which 
    * should be encode in "UTF-8": 
    * 
    * param-1) "joke": The text of the joke. 
    * 
    * param-2) "author": The author of the joke. 
    * 
    * URL: http://simexusa.com/aac/addJoke.php? 
    * 
    * @param joke 
    *   The Joke to be uploaded to the server. 
    * 
    */ 
    protected void uploadJokeToServer(Joke joke) { 
     // TODO 
    } 

}   

ここはadvanced.xmlです。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     > 
     <Button 
      android:id="@+id/addJokeButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Add Joke" 
      /> 
     <EditText 
      android:id="@+id/newJokeEditText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:hint="Enter Joke" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/jokeListViewGroup" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     > 

    </LinearLayout> 
</LinearLayout> 

更新:少し変更されたコード、Layout.params()が追加されましたが、TextViewは表示されません。

答えて

0

問題が解決されて....これはとても愚かだった.... 私はアンドロイドを追加するのを忘れ:ルート要素のためのオリエンテーション=「縦の」..コードを見てください。私はStackOverflowのに慣れ

は....私は

怠惰はあなたに答え、私は質問をするたびに一緒にいくつかの良いヒントを与えたすべての偉大な開発者をありがとうございました。

0

2つの理由; TVビューの高さと幅を直接またはレイアウトパラメータを使用して設定する必要があります。この後、setContentViewを再度呼び出す必要がありますが、追加するレイアウトはルートレイアウトの子ですルートレイアウトへの参照を取得してから、適切な子レイアウトにTVビューを追加してから、ContentView(ルートレイアウト)を設定する必要があります。

+0

ビューツリーが常に同じ場合(たとえさらに「葉」があっても)、setContentView()を再度呼び出す必要がありますか? – bigstones

+0

私は最後の文を理解できませんでした....私はルートレイアウトの子にTextViewを追加しました....これは問題ですか? – naquiuddin

+0

私は子レイアウトへの参照を与えて、TextViewウィジェットを追加しました。コードを参照してください。しかし、まだ表示されません... TextViewを動的に追加するときにheightとwidthパラメータを設定する必要がありますか? – naquiuddin

0

を確認する場合は、ビューが存在する場合は、階層ビューアを使用してください。これを確認してください:http://developer.android.com/guide/developing/tools/hierarchy-viewer.html

現在のすべてのビューとその配置方法が表示されます。これは、このようなデバッグで多くの助けになるはずです!私は非常に愚かだった

+0

heirarchyviewerを使用しようとしましたが、adb.exeが見つからないというエラーが表示されます。新しいSDK R8 ... adbの場所がplatform-toに変更されましたオール・オール・ソリューションズ。助けてください。 – naquiuddin

+0

ええ、パスにそのディレクトリを追加する必要があります。ソリューションに関する私のブログへの恥知らずのリンク:http://androblip.huiges.nl/2011/01/10/hierarchy-viewer-problems/ – Nanne

+0

それは全く恥知らずです.... ..非常に簡単な解決法です。 – naquiuddin

関連する問題