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....
How to pass intent in adapter class
Change this
Intent intent=new Intent(context,crunches.class);
context.startActivity(intent);
To
v.getContext().startActivity(new Intent(v.getContext(), crunches.class));
Hope it helps.!
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(...);
}
});
}
}
How to pass an intent from Adapter to Activity without intent.addFlags?
I learnt there no way to do it without adding flags.
But instead of this code,
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Details.class);
intent.putExtra("pid", pid);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
}
});
change it to this. problem solved !
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Details.class);
intent.putExtra("pid", pid);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
Unable to use intent in adapter
let see:
((Activity)mContext).finish();
The log say mContext is kind of FilePickerDelegate. So check your Adapter init function. I think you pass context as "this", but "this" reference to FilePickerDelegate. So change it to YourActivity.this ( with activity) or getActivity( in fragment)
Start Intent from Adapter
Since you are receiving the context into the constructor of your Adapter:
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
use this context to start the Intent
, for example :
Intent intent = new Intent(mContext, OtherActivity.class);
mContext.startActivity(intent);
Related Topics
How to Resolve Java.Lang.Noclassdeffounderror: Javax/Xml/Bind/Jaxbexception
How to Create Comma Separated String in Single Quotes from Arraylist of String in Java
Javac: File Not Found: First.Java Usage: Javac <Options> <Source Files>
Adb Cannot Connect to Daemon At Tcp:5037
Password Validate 8 Digits, Contains Upper, Lowercase, and a Special Character
How to Sort Json Object in Java
How to Get Image Height and Width Using Java
How to Acknowledge Current Offset in Spring Kafka for Manual Commit
How to Exclude a Dependency from Parent'S Project in Maven
Create List of Object from Another Using Java 8 Streams
Java H2 In-Memory Database Error: Table Not Found
Broadcast Receiver Is Not Working When Application Is Removed from Background
Testing Two Json Objects for Equality Ignoring Child Order in Java
Escaping New Line Character Within SQL Query
Recyclerview Not Calling Oncreateviewholder or Onbindview