我的意图有问题,它不起作用

时间:2019-08-14 10:48:22

标签: android android-intent

我需要我的应用程序帮助,我正在使用listView和适配器创建音乐应用程序,image for main activity 我使用imagebutton转到详细信息活动,但是当我单击它时没有任何反应

这是主要活动代码

package com.example.musicano;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     ArrayList<Song> songs=new ArrayList<Song>();
        songs.add(new Song("See You Again","Wiz Knalifa"));
        songs.add(new Song("Sorry","Justin Bieber"));
        songs.add(new Song("Uptown Funk","Mark Ronson "));
        songs.add(new Song("Blank Space","Taylor Swift"));
        songs.add(new Song("Shake It Off","Taylor Swift"));
        songs.add(new Song("Lean On","Major Lazer"));
        songs.add(new Song("Hello","Adele"));
        songs.add(new Song("Roa","Kary Perry"));
        songs.add(new Song("Sugar","Sugar"));
        songs.add(new Song("All About That Bass","Meghan Trainor"));
        songs.add(new Song("Dark Horse","Katy Perry"));
        songs.add(new Song("Counting Stars","Onerepublic"));
        songs.add(new Song("Baby","Justin Biebe"));
        songs.add(new Song("Chandelier","Meghan Sia"));
        ListView listView=(ListView) findViewById(R.id.list);
        SongAdapter adapter=new SongAdapter(this,songs);
        listView.setAdapter(adapter);

        LayoutInflater inflater = getLayoutInflater();
     View view= inflater.inflate(R.layout.list_item,listView,false);

        ImageButton playMusic = (ImageButton)view.findViewById(R.id.playButton);
        playMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Details.class);
                startActivity(intent);
            }
        });

    }

}

,自定义适配器代码为

package com.example.musicano;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class SongAdapter extends ArrayAdapter<Song> {
    public SongAdapter (Activity context, ArrayList<Song>songs){
        super(context,0,songs);
    }
@Override
    public View getView(int position, View convertView, ViewGroup parent){
        Song cureentSong=getItem(position);
        View listItemView=convertView;
        if(listItemView==null){
            listItemView= LayoutInflater.from(getContext()).inflate(R.layout.list_item,parent,false);
        }
    TextView songName=(TextView) listItemView.findViewById(R.id.text_view_song_name);
    TextView artistName=(TextView) listItemView.findViewById(R.id.text_view_artist_name);
    ImageButton playMusic=(ImageButton) listItemView.findViewById(R.id.playButton);
    songName.setText(cureentSong.getSongName());
    artistName.setText(cureentSong.getArtistName());
    return listItemView;
}

}

和activity_main.xml包含

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list"
    tools:context=".MainActivity">
</ListView>

和list_item布局包含

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:id="@+id/relativlayout"

    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view_song_name"
        tools:text="one"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="#232F34"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text_view_artist_name"
        tools:text="two"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingBottom="16dp"
        android:textAppearance="?android:textAppearanceSmall"
        android:textColor="#232F34"/>
    </LinearLayout>
    <ImageButton
        android:id="@+id/playButton"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/play"
        android:layout_marginRight="26dp"
        android:layout_marginTop="26dp"
        />
</RelativeLayout>

就是这样

1 个答案:

答案 0 :(得分:1)

执行view.findViewById的原因是什么,该按钮位于MainActivity本身中? 请尝试以下操作,并检查是否可行? 希望这会有所帮助

ImageButton playMusic = (ImageButton)findViewById(R.id.playButton);
        playMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Details.class);
                startActivity(intent);
            }
        });

如果图像按钮位于list_item_layout中,则将点击侦听器添加到adapter中,而不是MainActivity中。然后它应该工作。 在适配器代码中添加以下内容:

ImageButton playMusic=(ImageButton) listItemView.findViewById(R.id.playButton);
playMusic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext,Details.class);
                    intent .addFlags(FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            });

使用您的Adapters构造函数中使用的上下文创建实例

private Context mContext;

并在适配器的构造函数中将其分配给上下文,如下所示:

this.mContext = context

希望这会有所帮助。