Question

I have a "movie" class and a public function getName(), but the function doesn't return anything, and the logcat is just blank.

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 ;
    }

}

This is an activity which uses this method - the list always has blank entries.

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);
    }

}

the code which generate objects and add values to it

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();    

            }
        });

    }

}
Was it helpful?

Solution 2

The problem was in taking the data from the user in the MovieReg_activity

and its solved by putting those two lines

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

inside the button listener to be like this

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();    

            }
        });

OTHER TIPS

You are setting the TAG parameter for the logs to the first string, try:

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

And then set your logcat filter to DEBUG

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top