Start Intent in Adapter

前端 未结 8 1678
清酒与你
清酒与你 2021-02-02 13:32

I want to start a new activity from this base adapter.

public class EfficientAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList         


        
相关标签:
8条回答
  • 2021-02-02 13:53

    Set an intent for the class u need to open.In manifest file . Eg:

    <activity android:name=".openingclass"
    android:label="@string/app_name">
     <intent-filter>
     <action android:name="android.intent.action.OPEN" />
     <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
     </activity>
    

    Then

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
               Intent i=new Intent("android.intent.action.OPEN");
               StartActivity(i);
                      }
    
    0 讨论(0)
  • 2021-02-02 13:56

    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....

    0 讨论(0)
  • 2021-02-02 13:59

    Replace onClick method with this it may work I didn't tried:

    @Override
    public void onClick(View v) {    
        v.getContext().startActivity(new Intent(v.getContext(), NVirementEmmeteur.class)); 
    }
    
    0 讨论(0)
  • 2021-02-02 14:00

    I faced this problem previously and tried all of the suggestions above, but the only one helped me to start an activity from an adopter was the solution proposed by @Md. Sajedul Karim.

    I modified the code and used like this

    Intent intent = new Intent(context, NesneTani.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
    
    0 讨论(0)
  • 2021-02-02 14:04

    also you can do like, Here mContext is Your BaseAdpter Context Object

     Intent ieventreport = new Intent(mContext,Your.class);   
     mContext.startActivity(ieventreport);
    

    change your Constructer like, initialise

     public EfficientAdapter(Context context, ArrayList<ComptePost> d){
    }
    
    0 讨论(0)
  • 2021-02-02 14:05

    Pass activity context from activity and use same context while calling Intent.

    From activity:

    yourview.setAdapter(new EfficientAdapter(this));
    

    within adapter:

     private Context mContext;
    
    
      public EfficientAdapter(Context c) {
            mContext = c;
    
        }
    

    Now use mContext to call Intent.

    startActivity(new Intent(mContext, NVirementEmmeteur.class));
    
    0 讨论(0)
提交回复
热议问题