How to start Activity in adapter?
Just pass in the current Context to the Adapter constructor and store it as a field. Then inside the onClick you can use that context to call startActivity().
pseudo-code
public class MyAdapter extends Adapter {
private Context context;
public MyAdapter(Context context) {
this.context = context;
}
public View getView(...){
View v;
v.setOnClickListener(new OnClickListener() {
void onClick() {
context.startActivity(...);
}
});
}
}
Start Intent in Adapter
you have passed context of activity in constructor so you can also use;
activity.startActivity(new Intent(activity, NVirementEmmeteur.class));
check here is sample code you get idea what to do:
setadapter like : adapter = new MyArrayAdapter(MainActivity.this, COUNTRIES);
adapter code:
package com.example.testapp;
import com.example.main.util.testActivity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
class MyArrayAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context mcon;
private String[] COUNTRIES_;
public MyArrayAdapter(Context con, String[] countries) {
// TODO Auto-generated constructor stub
mcon = con;
COUNTRIES_ = countries;
mInflater = LayoutInflater.from(con);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return COUNTRIES_.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.my_spinner_style, null);
holder = new ListContent();
holder.line = (LinearLayout) v.findViewById(R.id.line_);
holder.name = (TextView) v.findViewById(R.id.textView1);
holder.name1 = (TextView) v.findViewById(R.id.textView2);
holder.name2 = (ImageView) v.findViewById(R.id.imageView1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.name.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
holder.name1.setText("" + Html.fromHtml("" + COUNTRIES_[position]));
holder.line.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mcon.startActivity(new Intent(mcon, testActivity.class));
}
});
return v;
}
}
class ListContent {
TextView name;
TextView name1;
ImageView name2;
LinearLayout line;
}
Edited:
if your are use this constructor: then list.setadapter(new EfficientAdapter(myactivity.this));
public EfficientAdapter(Context context) {
this.context = context;
}
then use : context.startActivity(new Intent(context, NVirementEmmeteur.class));
if you use this construdtor list.setadapter(new EfficientAdapter(myactivity.this, ComptePostarray));
public EfficientAdapter(Activity a, ArrayList<ComptePost> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// imageLoader = new ImageLoader(activity.getApplicationContext());
imageLoader=new ImageLoader(activity.getApplicationContext());
}
then use activity.startActivity(new Intent(activity, NVirementEmmeteur.class));
Hope you understud....
Starting an Activity from an Adapter
change this
activity.startActivity( new Intent(activity,BasketActivity.class));
to
context .startActivity( new Intent(context,BasketActivity.class));
OR Create asynck task in adapter.
How to start Activity in my adapter?
You could try:
post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(),asabani_cat.class));
}
});
Start Activity in Adapter
Probably context
is null
:
Use single constructor to pass String Array and Context :
public CardViewDataAdapter_smiley(String[] myDataset,Context context) {
this.mDataset = myDataset;
this.context = context;
}
OR
with-out passing Context in constructor use v.getContext()
to start Activity on Click:
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),SMS_Smiley.class);
v.getContext().startActivity(intent);
}
How to open a new activity from the adapter?
Don't dispose passed context to your adapter
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<Phone> phones;
private Context context;
public DataAdapter(Context context, List<Phone> phones) {
this.context = context;
this.phones = phones;
this.inflater = LayoutInflater.from(context);
}
// Remainder code
}
Then use context
to start new activity
@Override
public void onBindViewHolder(DataAdapter.ViewHolder holder, int position) {
final Phone phone = phones.get(position);
holder.imageView.setImageResource(phone.getImage());
holder.nameView.setText(phone.getName());
holder.companyView.setText(phone.getCompany());
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("click: ", phone.getClickbutton());
context.startActivity(new Intent(this, Information.class));
}
});
}
How to pass intent from Adapter class to other activity but my variable is in other activity
You can have couple of options to solve this:
Option 1:
If you want to start "ProfilWarehouseActivity" activity from the adapter, then you need to pass the "EmailHolder" from "WarehouseActivity" to your adapter by either its constructor, or a setter:
Passing String into Adapter Constructor
final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this, list, EmailHolder);
Passing String using a Setter
In "ProfilWarehouseActivity" activity:
mAdapter.setEmailHolder(EmailHolder);
In WarehouseAdapter adapter:
private String mEmailHolder;
public void setEmailHolder(String emailHolder) {
this.mEmailHolder = emailHolder;
}
....
private void passid(String idItem) {
Intent intent = new Intent(context, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID, idItem);
intent.putExtra("EmailHolder", mEmailHolder);
//i think this is for get my EmailHolder from WarehouseActivity to pass it with Intent from this Adapter class
context.startActivity(intent);
}
Option 2:
The other option is to create a listener and implement it in "ProfilWarehouseActivity", And pass the itemId as a parameter to the listener callback. Then let your "ProfilWarehouseActivity" call the "WarehouseActivity" instead of the WarehouseAdapter whenever this listener is triggered.
In WarehouseAdapter adapter:
public interface ItemClickListener {
public void onItemClick(int idItem);
}
ItemClickListener mItemClickListener;
public void setItemClickListener(ItemClickListener listener) {
mItemClickListener = listener;
}
private void passid(String idItem) {
if (mItemClickListener != null)
mItemClickListener.onItemClick(idItem);
}
In "ProfilWarehouseActivity" activity:
class ProfilWarehouseActivity extends AppCompatActivity implements WarehouseAdapter.ItemClickListener {
private String EmailHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warehouse);
Bundle extras = getIntent().getExtras();
EmailHolder = extras.getString("emailuser");
}
@Override
onItemClick(int itemId) {
Intent intent = new Intent(this, ProfilWarehouseActivity.class);
intent.putExtra(Konfigurasi_Warehouse.WAREHOUSE_ID, idItem);
intent.putExtra("EmailHolder", EmailHolder);
startActivity(intent);
}
...
final WarehouseAdapter mAdapter = new WarehouseAdapter( WarehouseActivity.this,list);
mAdapter.setItemClickListener(this);
How to start activity from RecyclerView adapter in fragment
Pass contaxt to recyclerview adapter constructor like this
Context context;
MyAdapter(Context context, .....){
this.context=context;
}
Call Activity
context.startActivity(......);
how to open activity from adapter in kotlin
Just replace the error line with
context.startActivity(intent)
Related Topics
Java/Android - How to Print Out a Full Stack Trace
Onclick Event Is Not Triggering | Android
Java - Convert String to Valid Uri Object
Create Pdu for Android That Works with Smsmessage.Createfrompdu() (Gsm 3Gpp)
Android Simpledateformat, How to Use It
How to Draw a Filled Triangle in Android Canvas
How to Check Wifi or 3G Network Is Available on Android Device
How to Kill Currently Running Task in Android
Ksoap2 Org.Xmlpull.V1.Xmlpullparserexception Expected Start_Tag Error
How to Set Up Intellij Idea for Android Applications
How to Use Weakreference in Java and Android Development
How to Avoid Garbage Collection Delays in Java Games? (Best Practices)
How to Parse JSON from a Java Httpresponse
Android Web Scraping with a Headless Browser
How to Get Current Location in Googlemap Using Fusedlocationproviderclient
How to Give an Imageview Click Effect Like a Button on Android