Skip to content
Advertisement

From Adapter into Intent

I have an Adapter for a ListView I have. Every item in that list has a Button, when the user presses that Button, it will go into another activity with info about that specific item’s info (info is gotten from it’s associated object).

How can I make it so when I press the Button, I will make a new Intent object, put user.getOfferID() into it and then start that activity? I tried doing the old fashioned way but it doesn’t let me write startActivity(intent);, says it doesn’t exist.

How can I do that?

Here is the Adapter:

package com.example.create4me;

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.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class UsersAdapter extends ArrayAdapter<UserItem> {
    public UsersAdapter(Context context, ArrayList<UserItem> users) {
        super(context, 0, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        UserItem user = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.userlayout, parent, false);
        }
        TextView username = (TextView) convertView.findViewById(R.id.userlayoutUsername);
        TextView timestamp = (TextView) convertView.findViewById(R.id.userlayoutTimestamp);
        TextView isComplete = (TextView) convertView.findViewById(R.id.isComplete);
        Button viewPurchase = (Button) convertView.findViewById(R.id.userlayoutViewBtn);
        username.setText(user.getUsername());
        timestamp.setText(user.getTimestamp());
        isComplete.setText(user.getIsComplete());
        viewPurchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //How can I make it so HERE I go into another acitivity that will also
                //get user.getOfferID() as a parameter?
                //The button works since the Toast is shown when I click it
                
                Toast.makeText(getContext(), "I was clicked", Toast.LENGTH_LONG).show();
            }
        });
        return convertView;
    }
}

Here is the UserItem class:

package com.example.create4me;

public class UserItem {
    String username, timestamp, offerID, isComplete;
    public UserItem(String username, String timestamp, String offerID, String isComplete){
        this.username = username;
        this.timestamp = timestamp;
        this.offerID = offerID;
        this.isComplete = isComplete;
    }
    public String getUsername(){
        return this.username;
    }
    public String getTimestamp(){
        return this.timestamp;
    }
    public String getOfferID(){
        return this.offerID;
    }
    public String getIsComplete(){
        return this.isComplete;
    }
}

Advertisement

Answer

startActivity(intent) doesn’t come with free, you need a context. You can call startActivity(intent) inside an ordinary activity class because it has built in context variable. Because you create a new class UsersAdapter, you need to store context that is passed from the constructor by creating a new instance variable.

private Context context;

Then assign it in the constructor.

public UsersAdapter(Context context, ArrayList<UserItem> users) {
    super(context, 0, users);
    this.context = context;
}

Now inside the setOnClickListener you can call startActivity using the context and Android Studio won’t show error.

context.startActivity(yourIntent);
Advertisement