从适配器到意图

From Adapter into Intent

我有一个用于 ListView 的适配器。该列表中的每个项目都有一个按钮,当用户按下该按钮时,它将进入另一个 activity,其中包含有关该特定项目信息的信息(信息是从它的关联对象中获取的)。

我怎样才能在按下按钮时创建一个新的 Intent 对象,将 user.getOfferID() 放入其中,然后启动 activity?我尝试过老式的方式,但它不允许我写 startActivity(intent);,说它不存在。

我该怎么做?

这是适配器:

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

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

startActivity(intent) 不是免费的,你需要一个上下文。您可以在普通 activity class 中调用 startActivity(intent),因为它内置了上下文变量。因为你创建了一个新的class UsersAdapter,你需要通过创建一个新的实例变量来存储从构造函数传递过来的上下文。

private Context context;

然后在构造函数中赋值

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

现在在 setOnClickListener 中,您可以使用上下文调用 startActivity,AndroidStudio 不会显示错误。

context.startActivity(yourIntent);