使用 ListAdapter 在 View 方法中显示 Toast

Showing Toast in View method with ListAdapter

我用 Adapter 创建了一个列表视图,并且我有一个切换按钮,我希望它可以显示祝酒词,但它不起作用...

这是我在 AppCompactActivity 中的代码

 protected void onCreate(Bundle savedInstanceStated){
    super.onCreate(savedInstanceStated);
    setContentView(R.layout.list_activity);

    QuestionsListAdapter q = new QuestionsListAdapter(this, questions, picture);
    list = (ListView) findViewById(R.id.list);
    list.setAdapter(q);

}

这是我的 ListAdapter 中的 getView 函数

public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    final View rowView=inflater.inflate(R.layout.pictures_question, null,true);

    ImageView pic = (ImageView) rowView.findViewById(R.id.q_picture);
    question = (TextView) rowView.findViewById(R.id.txt_q_question);
    btnPlayer = (ToggleButton) rowView.findViewById(R.id.btn_q_player);

    question.setText(questions[position]);
    pic.setImageResource(picture[position]);

    teks  = question.getText().toString();

    btnPlayer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (btnPlayer.isChecked()){
                Toast.makeText(context.getApplicationContext(), teks + " ON", Toast.LENGTH_SHORT);
            } else if (!btnPlayer.isChecked()){
                Toast.makeText(context.getApplicationContext(), teks + " OFF", Toast.LENGTH_SHORT);
            }
        }
    });

    return rowView;

};

所以,希望有人能解决这个问题

您忘记在 Toast

中添加 show() 方法

show()

void show () You can display the toast notification with show()

试试这个

 btnPlayer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (btnPlayer.isChecked()){
                Toast.makeText(context.getApplicationContext(), teks + " ON", Toast.LENGTH_SHORT).show();
            } else if (!btnPlayer.isChecked()){
                Toast.makeText(context.getApplicationContext(), teks + " OFF", Toast.LENGTH_SHORT).show();
            }
        }
    });

完整答案。抱歉我的英语不好。

   public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    final View rowView=inflater.inflate(R.layout.pictures_question, null,true);

    ImageView pic = (ImageView) rowView.findViewById(R.id.q_picture);
    question = (TextView) rowView.findViewById(R.id.txt_q_question);
    btnPlayer = (ToggleButton) rowView.findViewById(R.id.btn_q_player);
    question.setText(questions[position]);
    pic.setImageResource(picture[position]);
    btnPlayer.setTag(position);

    btnPlayer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int positionItem = (int) v.getTag();
            if (((ToggleButton) v).isChecked()){
                Toast.makeText(context.getApplicationContext(), questions[positionItem] + " ON", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context.getApplicationContext(), questions[positionItem] + " OFF", Toast.LENGTH_SHORT).show();
            }
        }
    });

    return rowView;

};