-3

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>)'
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mark Barton
  • 847
  • 6
  • 15
  • The perks of a ListView is that is has an interface where you can call onItemClickListener inside your Activity, and then inside the onClick method use a switch statement for the id of the view you want and put the Intent in there. Related: http://stackoverflow.com/questions/18405299/onitemclicklistener-using-arrayadapter-for-listview – Bradley Wilson Mar 14 '17 at 12:16

4 Answers4

0
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.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
0

This is what i have used in one of my application.

 LocationAdapter adapter = new LocationAdapter(getApplicationContext(), R.layout.row, result);
                //set the locationList (listview) to use adapter initialised above
                locationList.setAdapter(adapter);
                //Set up onClickListner
                locationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //get the position of the row. So which row was clicked
                        LocationModel locationModel = result.get(position);
                        //initialise intent, this click event takes the user to locationDetails Activity
                        Intent intent = new Intent(ListActivity.this, LocationDetails.class);
                        //passing extra value. in this case passing of locationModel
                        intent.putExtra("locationModel", new Gson().toJson(locationModel));
                        //start activity
                        startActivity(intent);
                    }
                });
chirag90
  • 2,211
  • 1
  • 22
  • 37
0

In adapter class "this" does not returen class object. set OnItemClickListener with list object

 listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent startMenuActivity = new Intent(MyActivity.this, Menu_Activity.class);
            startActivity(startMenuActivity);
        }
    });
0

Change this:

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

            @Override
            public void onClick(View v) {

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

For this:

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

            @Override
            public void onClick(View v) {

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

Diference:

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

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

Remember:

When you "say" this into anonymous class, you are refering to her (anonymous object class), not your "main class".

See more Anonymous Classes - Oracle Doc

Community
  • 1
  • 1
Peter R
  • 384
  • 1
  • 11