0

I have a textview in a listview on clicking which it should perform some activity. Currently,I am writing onClick of textview inside getView method of custom adapter class. On click of textview , I am trigerring a method in my Activity class . But that Activity has that variable value as NULL eventhough its already been initialised in onCreate of Activity. Here's my code:

Adapter class:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     final int pos = position;
        Item item = (Item) getItem(position);
        TextView textView = (TextView) view
                .findViewById(R.id.tv_song_title);
        textView.setText(item.text);

        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                  MainActivity main = new MainActivity();
                main.songPicked(pos); //calling act class method
            }
        });

Activity class:

    private MusicService musicSrv;
    public void songPicked(int position) {  //method called
    if (musicSrv!=null) //is null .Why??
    {
    musicSrv.setSong(position);
    songName = musicSrv.playSong();

    }
}

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
            songAdt = new SongAdapter();
    songAdt.setRows(rows);
    songView.setAdapter(songAdt);
    playMusic();
        }
    public void playMusic() {

    if (playIntent == null) {
        playIntent = new Intent(this, MusicService.class);
        startService(playIntent);
        bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE);

    }

}
  // connect to the service
private ServiceConnection musicConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        MusicBinder binder = (MusicBinder) service;
        // get service
        musicSrv = binder.getService();
        // pass list
        musicSrv.setList(songList);
        musicBound = true;
        Log.i("LEAKTEST", "Connected to instance " + this.toString());

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

        musicBound = false;
        musicSrv = null;

    }
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
Siju
  • 2,585
  • 4
  • 29
  • 53

1 Answers1

0

Use listeners instead of passing the whole activity.

The main idea is

public interface SmthListener() {
    void onSmthHappens(smthParams);
}

public class WhoNotify {
    SmthListener mListener;

    public WhoNotify(SmthListener listener) {
        mListener = listener;
    }

    public void smthHappensInMyClass() {
        mListener.onSmthHappens(smthParams);
    }

}


public class WhoListene implements SmthListener {
    @Override
    public void onCreate(...) {
        ... new WhoNotify(this);
    }

    @Override
    void onSmthHappens(smthParams) {
        // do stuff;
    }
}
dilix
  • 3,761
  • 3
  • 31
  • 55