Darshan Gowda
Darshan Gowda

Reputation: 5216

How to start an Activity from a class extending BaseAdapter?

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

Upvotes: 2

Views: 11427

Answers (5)

Osama Ibrahim
Osama Ibrahim

Reputation: 1021

   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);

        }
    });

Upvotes: 1

PANKAJ SHARMA
PANKAJ SHARMA

Reputation: 27

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);

}

Upvotes: 1

Hamid Shatu
Hamid Shatu

Reputation: 9700

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,int pos, long arg3) {
        Intent intent= new Intent(currentClass.this,secondActivity.class);
        currentClass.this.startActivity(intent);
        finish();                       
    }
});

Upvotes: 0

nKn
nKn

Reputation: 13761

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;
}  

Upvotes: 1

Raghunandan
Raghunandan

Reputation: 133560

Use Activity context

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

Upvotes: 12

Related Questions