How to start an Activity from a class extending BaseAdapter?

11,181

Solution 1

Use Activity context

Intent intent = new Intent(context,AnotherActivity.class);
context.startActivity(intent);

Solution 2

You can also do like this ..

public void onClick(View v){
Intent yourIntent = new Intent(context,AnotherActivity.class);
yourIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
v.getContext().startActivity(yourIntent);

}

Solution 3

In order to not overwork the Adapter, I'd register each row for a context menu using registerForContextMenu(convertView). This way, you'll have to implement the onCreateContextMenu() and onContextItemSelected() outside the Adapter, probably within the Activity you're populating your ListView, and define the startActivity() inside that Activity, where it will work just fine.

An example follows:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  menu.setHeaderTitle("Context menu");
  menu.add(Menu.NONE, 0, 0, "Start the new Activity");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
  final int mId = item.getItemId();

  switch (mId) {
    case 0:         // Entrar a un canal
      final Intent intent = new Intent(context, MySecondActivity.class);
      startActivity(intent);

      break;

    default:
      break;
  }

  return true;
}  

Solution 4

   TextView mealName = (TextView) view.findViewById(R.id.mealNameDietTXT);
    mealName.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            SessionClass.subMealID=mealsArray.get(position).getMealComponentId();
            Intent i = new Intent(dietRadioAdapter.this.context, dietDetails.class);
           dietRadioAdapter.this.context.startActivity(i);

        }
    });
Share:
11,181
Darshan Gowda
Author by

Darshan Gowda

Updated on June 05, 2022

Comments

  • Darshan Gowda
    Darshan Gowda almost 2 years

    I am using the following code to generate a custom ListView. Everything is working fine, but when user clicks on the listview, i cannot make it to go to another class as Intents cannot be used in non Activity class. Is there a way to start an activity for the below code???

    public class CustomAdapter extends BaseAdapter  {
    String[] result;
    Context context;
    int[] imageId;
    private static LayoutInflater inflater = null;
    
    public CustomAdapter(MainActivityList mainActivity, String[] prgmNameList,
            int[] prgmImages) {
        // TODO Auto-generated constructor stub
        result = prgmNameList;
        context = mainActivity;
        imageId = prgmImages;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return result.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;
    }
    
    public class Holder {
        TextView tv;
        ImageView img;
    }
    
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Holder holder = new Holder();
        View rowView;
        rowView = inflater.inflate(R.layout.program_list, null);
        holder.tv = (TextView) rowView.findViewById(R.id.textViewlist);
        holder.img = (ImageView) rowView.findViewById(R.id.imageViewlist);
        holder.tv.setText(result[position]);
        holder.img.setImageResource(imageId[position]);
    
        rowView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch(position)
                {
                case 0:
                {
                    //Intent is not working here
                                        //Here i need some help 
                }
                }
            }
        });
        return rowView;
    }
    
    }
    

    I am stuck in this part of the project, please do help me..