Search code examples
androidlistviewfirebaseonclicklistenercustom-adapter

Moving to a new activity when ListViewItem is clicked


I'm creating a sort of sign in system for my app and I have it working to a point where the user can add their details and choose them from a list.

the user inputs their information into a UserInput class and this information is then stored in a Firebase database. The data is then displayed in a ListView item but is first put through a CustomAdapter class. This class is where I'm having my problems.

As it stands the user is able to interact with this listview through a setOnClickListener(). I have the program display a Toast when the item from the list is selected and it shows it no problem. What I'm trying to figure out now is how to get it so that when a user clicks the listview item (which includes their name and details) it immediately takes them to a main menu activity called Menu_Activity.java

Messages Gradle Build

Error:(71, 44) error: no suitable constructor found for Intent(<anonymous OnClickListener>,Class<Menu_Activity>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
error:cannot find symbol method startActivity(Intent)

CustomAdapter.java

package uk.ac.napier.newsreader.Details_UI;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import uk.ac.napier.newsreader.Home_Screen_Activity;
import uk.ac.napier.newsreader.Menu_Activity;
import uk.ac.napier.newsreader.R;
import uk.ac.napier.newsreader.Details_UserInput.User;
import uk.ac.napier.newsreader.Routes_Activity;

/**
 * Created by MarkB on 14/03/2017.
 */

public class CustomAdapter extends BaseAdapter {
    Context c;
    ArrayList<User> users;

    public CustomAdapter (Context c, ArrayList<User> users) {
        this.c = c;
        this.users = users;
    }

    @Override
    public int getCount() {
        return users.size();
    }

    @Override
    public Object getItem(int position) {
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null)
        {
            convertView = LayoutInflater.from(c).inflate(R.layout.model,parent,false);
        }

        TextView nameTxt = (TextView) convertView.findViewById(R.id.nameTxt);
        TextView ageTxt = (TextView) convertView.findViewById(R.id.ageTxt);
        TextView weightTxt = (TextView) convertView.findViewById(R.id.weightTxt);

        final User u = (User) this.getItem(position);

        nameTxt.setText(u.getName());
        ageTxt.setText(u.getAge());
        weightTxt.setText(u.getWeight());

        //ONITEMCLICK
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //Toast.makeText(c,u.getName(), Toast.LENGTH_SHORT).show();

                Intent startMenuActivity = new Intent(this, Menu_Activity.class);
                startActivity(startMenuActivity);
            }
        });

        return convertView;
    }
}

the problem appears when creating the Intent with "(this, Menu_Activity.class);" flagging an error saying

Cannot resolve constructor 'Intent(anonymous android.view.View.OnClickListener, java.lang.Class<uk.ac.napier.newsreader.Menu_Activity>)'

Solution

  • Intent startMenuActivity = new Intent(c, Menu_Activity.class);
    

    here c is your context variable.

    Explanation:

    You are supposed to pass activity context as first argument.