类中的公共函数不能正常工作

时间:2014-02-18 22:50:00

标签: android class oop public-method

我有一个“movie”类和一个公共函数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();    

            }
        });

    }

}

2 个答案:

答案 0 :(得分:0)

您正在将日志的TAG参数设置为第一个字符串,请尝试:

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

然后将logcat过滤器设置为DEBUG

答案 1 :(得分:0)

问题在于从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();    

            }
        });