简体   繁体   English

如何在Intent中调用此方法?

[英]How call this method in a Intent?

What I have: I have a RecyclerView with images and each one of this images has an OnClickListener and an Intent 我所拥有的:我有一个带有图像的RecyclerView,每个图像都有一个OnClickListener和一个Intent。

What I want: I want to trigger a method (MethodIwantToCall) that is inside of another Activity when one of this pictures is clicked 我想要什么:单击此图片之一时,我想触发另一个Activity内的方法(MethodIwantToCall)

My question: How can I trigger this method on an Intent? 我的问题:如何在Intent上触发此方法? Please, I would really appreciate the answers with code example, I am new programming and there are many things I don't understand, thanks in advance 拜托,我非常感谢代码示例的回答,我是新编程人员,还有很多我不理解的事情,在此先感谢

Activity with the method I want to call 我想调用的方法的活动

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {


 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        MethodIwantToCall();
    }


public void MethodIwantToCall(){

            String Restaurant = "restaurant";
                mMap.clear();
                String url = getUrl(latitude, longitude, Restaurant);
                Object[] DataTransfer = new Object[2];
                DataTransfer[0] = mMap;
                DataTransfer[1] = url;
                Log.d("onClick", url);
                GetNearbyPlacesData getNearbyPlacesData = new GetNearbyPlacesData();
                getNearbyPlacesData.execute(DataTransfer);
                Toast.makeText(MapsActivity.this,"Nearby Restaurants", Toast.LENGTH_LONG).show();
            }
        });
    }

My RecyclerView adapter and where I want to call this method 我的RecyclerView适配器以及要在哪里调用此方法

 public class AdapterDatos extends RecyclerView.Adapter<AdapterDatos.ViewHolder> {

 @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.etiNombre.setText(listalugares.get(position).getNombre());
        holder.foto.setImageResource(listalugares.get(position).getFoto());

        holder.foto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == 0){
                    Intent myIntent = new Intent(context, MapsActivity.class);
                    context.startActivity(myIntent);

                }
            }
        });
    }
}

You can handle this by putting extras to your target activity's intent and, to make things easier, you can create a static method in your target activity that will return an intent to that activity with the previously mentioned extra. 您可以通过在目标活动的意图中添加额外内容来处理此问题,并且为了使事情变得更容易,您可以在目标活动中创建一个静态方法,该方法将通过前面提到的额外内容将意图返回给该活动。

Here is an example: 这是一个例子:

// --- MapsActivity ---
private static final String EXTRA_TRIGGER_METHOD = "trigger_method";

public static Intent getIntent(Context context, boolean triggerMethod) {
    Intent intent = new Intent(context, MapsActivity.class);
    intent.putExtra(EXTRA_TRIGGER_METHOD, triggerMethod);
    return intent;
}

private boolean triggerMethod;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    // here we will assign to triggerMethod the value you passed to the intent when we started this activity
    triggerMethod = getIntent().getBooleanExtra(EXTRA_TRIGGER_METHOD, false);

    if (triggerMethod) {
        methodIWantToCall();
    }
}

// --- AdapterDatos ---
holder.foto.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (position == 0) {
            final boolean triggerMethod = true;
            Intent intent = MapsActivity.getIntent(context, triggerMethod);
            context.startActivity(intent);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM