2014-02-18 179 views
0

我有一個「電影」類和公共職能的getName(),但功能不返回任何東西,和logcat的只是空白。公共功能不能正常工作

public class movie { 

    public String name45; 
    int dvd_no ; 

    public void addData(String name1 , int dvd_no1) 
    { 
       this.name45=name1 ; 
      this.dvd_no = dvd_no1 ; 

      Log.d("constructor name1", name1); 

      Log.d("constructor name45", name45); 

    } 

    public String getName() 
    { 
     return name45 ; 
    } 

} 

這是一個使用此方法的活動 - 該列表始終有空白條目。

public class MoviesList extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.movieslist); 
     ListView lvAllMoviesList = (ListView)findViewById(R.id.allmovieslist); 

      ArrayList<String> moviesNames = new ArrayList<String>(); 
      // go through list of members and compare name with given name 
      for(movie movie : MovieReg_activity.movies) { 
      String name = movie.getName(); 
      Log.d("Movie Name list", movie.getName()); 
        moviesNames.add(name); 

      } 


     ArrayAdapter<String> AllMovieList = new ArrayAdapter<String>(MoviesList.this,android.R.layout.simple_list_item_1, moviesNames); 
     lvAllMoviesList.setAdapter(AllMovieList); 
    } 

} 

其產生的對象,並添加值,它

public class MovieReg_activity extends Activity { 

    public static List<movie> movies = new ArrayList<movie>(); 

    String movName ; 
    int dvdNo ; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mov_reg_layout); 
     EditText etmovie_name = (EditText)findViewById(R.id.etmovname); 
     EditText etdvd_no = (EditText)findViewById(R.id.etdvds); 
     Button btMovie_submit = (Button)findViewById(R.id.btmovsubmit); 

     movName= etmovie_name.getText().toString(); 
    // dvdNo = Integer.parseInt(etdvd_no.getText().toString()); // to string then to int :) 

     btMovie_submit.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       int x=0 ; 
       movie movie = new movie() ; 
       movie.addData(movName, dvdNo); 
       movies.add(x,movie); 
       x++ ; 
       int size =movies.size() ; 
       Toast.makeText(MovieReg_activity.this, "no of movies added :"+size , Toast.LENGTH_SHORT).show();  

      } 
     }); 

    } 

} 
+0

我們如何調試這解決了嗎?我們不知道你是如何使用它的。 –

+0

@DaveNewton抱歉..我將編輯後 – Fawzinov

+0

@DaveNewton我的意思通過的logcat來測試一下吧! – Fawzinov

回答

0

的問題是我n的用戶獲取數據的MovieReg_activity

其通過把這兩行

EditText etmovie_name = (EditText)findViewById(R.id.etmovname); 
movName= etmovie_name.getText().toString(); 

按鈕監聽器裏是這樣

public void onClick(View v) { 
       int x=0 ; 
       EditText etmovie_name = (EditText)findViewById(R.id.etmovname); 
       movName= etmovie_name.getText().toString(); 
       movies.add(new movie(movName , dvdNo)); 
       String name3= movie.getName() ; 
       x++ ; 
       int size =movies.size() ; 
       Toast.makeText(MovieReg_activity.this, "no of movies added :"+size , Toast.LENGTH_SHORT).show();  

      } 
     }); 
0

您設置日誌的第一串TAG參數的代碼,請嘗試:

Log.d("DEBUG", "constructor name1 " + name1); 
Log.d("DEBUG", "constructor name45 " + name45); 

然後設置您logcat的過濾器DEBUG

+0

沒有解釋缺乏返回值雖然。 –

+0

問題出在方法本身上。編輯問題 – Fawzinov

+0

儘管如此,它解釋了爲什麼他的logcat是空白的,並且可能很好地顯示它正在輸出一個值。 – NameSpace