【问题标题】:Textview click in listview [duplicate]Textview单击列表视图[重复]
【发布时间】:2014-06-24 16:06:46
【问题描述】:

我在列表视图中有一个文本视图,单击它应该执行一些活动。目前,我正在自定义适配器类的getView方法中编写textview的onClick。单击 textview 时,我在我的 Activity 类中触发了一个方法。但是,尽管它已经在 Activity 的 onCreate 中初始化,但该 Activity 的变量值为 NULL。这是我的代码:

适配器类:

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

活动类:

    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;

    }

【问题讨论】:

  • 如果MainActivityActivity 类。你所拥有的MainActivity main = new MainActivity(); 已经磨损了
  • 那我应该如何从Adapter类访问Activity类中的方法呢?
  • 更改适配器的构造函数以传递对活动的引用。
  • 您不能初始化扩展 Activity 的类,因为 Activity 有它自己的生命周期。只需在构造函数中传递对适配器的引用即可。
  • 你能举个例子吗?

标签: android listview textview


【解决方案1】:

使用listeners 而不是传递整个活动。

主要思想是

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

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多