Hamdy
Hamdy

Reputation: 25

I have a problem with intent it does not work

I need a help with my app, I am creating music app by using listView and adapter,image for main activity i use imagebutton to go to details activity but when i click on it no thing happen

this is main activity code

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

    }

}

and custom adapter code is

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

}

and activity_main.xml contain

<?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>

and list_item layout contain

<?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>

that is it

Upvotes: 0

Views: 91

Answers (1)

keshav kowshik
keshav kowshik

Reputation: 2374

What is reason for doing view.findViewById, the button is in MainActivity itself right? Try the following and check if this works? Hope this helps

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

If the image button is in list_item_layout then add the click listener in the adapter instead of MainActivity. Then it should work. Add the following in your adapter code:

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

Use the context used in your Adapters constructor create a instance

private Context mContext;

and inside your adapter's constructor assign it to context as shown below:

this.mContext = context

Hope this helps.

Upvotes: 1

Related Questions