suresh
suresh

Reputation: 417

How to call an activity from ListView Adapter?

I have custom ListView,my ListView contains one button, if we click on button i want to go another activity with some data.I used following code,

holder.mMore.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == event.ACTION_DOWN){
                  Intent moreIntent=new Intent(getContext(),SelectItemDesp.class);
                  v.getContext().startActivity(moreIntent);

                }
                return false;
            }
        });

it is showing error.pls help me

Upvotes: 2

Views: 7322

Answers (5)

Bedant
Bedant

Reputation: 326

Try This. It should Work

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

Upvotes: 0

BenjaminButton
BenjaminButton

Reputation: 334

I'll assume that you have written a class for your ListView adapter. Let's just name this class quickly: MyListViewAdapter. And in this class you most probably have a constructor. It could look like this:

public MyListViewAdapter (Context context, ArrayList<String> myList) {
super (context, R.layout.my_layout, R.id.my_text_view, myList);

Now the context is what you need to start a new Activity because a ListView adapter which extends an ArrayAdapter cannot start an Activity because its not derived from the Activity-class. So this is how you start an Activity then:

context.startActivity(context, GoToClass.class);

Just make sure to add a global but private variable to your code (private Context context) and add this to your constructor this.context = context and if you create the object you have to put MyListViewAdapter m = new MyListViewAdapter(CurrentClass.this, myListFullOfStrings);

Upvotes: 1

Vijay
Vijay

Reputation: 2005

I am also using customlistview and also have one delete button in that listview and i have done something like below and it work for me.

and one thing my class extends ArrayAdapter

public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;

    if(convertView == null)
    {
        holder = new ViewHolder();

        convertView = inflater.inflate(R.layout.row_layout_mymedicine, null);


        holder.btnDelete = (Button)convertView.findViewById(R.id.btnDelete);

        holder.btnDelete.setOnClickListener(this);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder)convertView.getTag();
    }

    return convertView;
}



then onClick() method i have do

public void onClick(View v)
{
switch(v.getId())
{
case R.id.btnDelete:
getContext().startActivity(new Intent(getContext(),DeleteActivity.class));
break;
default:
break;

}
}

Upvotes: 0

Aman Aalam
Aman Aalam

Reputation: 11251

You're invoking the new activity from an anonymous inner class, anything like this would refer to this anonymous class only.

Use MyClass.this as Vinayak suggested and if error persists, post the logcat here

Upvotes: 0

Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40503

Replace

 Intent moreIntent=new Intent(getContext(),SelectItemDesp.class);
              v.getContext().startActivity(moreIntent);

With

Intent lObjIntent = new Intent(getApplicationContext(), SelectItemDesp.class);
startActivity(lObjIntent);
finish();

Upvotes: 0

Related Questions