How can I launch an intent by clicking on the child element of an expandable List?

1

I want to launch an intent but it gives me an error and I do not locate the why. Any suggestions I get the error when creating the intent:

 @Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    tempChild = (ArrayList<String>) Childtem.get(groupPosition);
    TextView text = null;
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.childrow, null);
    }
    text = (TextView) convertView.findViewById(R.id.textView1);
    text.setText(tempChild.get(childPosition));
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dato = tempChild.get(childPosition);
            Toast.makeText(activity, dato,
                    Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(NewAdapter.this, Ficha.class);
            intent.putExtra("id", id);
            activity.startActivity(intent);
        }
    });
    return convertView;
}

NewAdapter.java is a class that extends from BaseExpandableListAdapter.

    
asked by tripossi 14.02.2017 в 13:41
source

2 answers

2

You are using as intent context the adapter. I suppose that 'activity' is the activity from which the adapter is called, so it would look like this:

Intent intent = new Intent(activity, Ficha.class);
intent.putExtra("id", id);
activity.startActivity(intent);
    
answered by 14.02.2017 / 13:48
source
2

What is definitely needed is the context of the Activity:

//Intent intent = new Intent(NewAdapter.this, Ficha.class);
Intent intent = new Intent(activity, Ficha.class);
intent.putExtra("id", id);
activity.startActivity(intent);

but it is important to mention that to do this, since it is done within a class that extends from BaseExpandableListAdapter :

public class NewAdapter extends BaseExpandableListAdapter {

you need a method that receives the context and that method is setInflater() :

public Activity activity;

public void setInflater(LayoutInflater mInflater, Activity act) {
    this.minflater = mInflater;
    activity = act;
}

The class should receive the context in some way to use it in Intent .

    
answered by 14.02.2017 в 17:40