简体   繁体   中英

Can't start Activity from OnClickListener “method startActivity(Intent, int) is undefined for the type”

I am populating a Scrollview using items retrieved from a SQLite database, and am dynamically generating an OnClickListener for each row using the code below. I want the code in the OnClickListener to return control to the Activity which called it (MealActivity) using an Intent, passing back the id of the item which was clicked on.

I've attempted this using the 2nd code segment below, but I'm getting a compile error: "The method startActivity(Intent, int) is undefined for the type OnClickListenerSelectPresetItem".

How can I invoke the original Activity from my OnClickListener?

public class SelectPresetItemActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_preset_item);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        readRecords();
    }

    public void readRecords() {
        LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
        linearLayoutRecords.removeAllViews();

        List<preset_item> preset_item = new TableControllerPresetItem(this).read();

        if (preset_item.size() > 0) {

            for (preset_item obj : preset_item) {

                long id = obj.id;
                String PresetDesc = obj.preset_desc;
                int PresetMinutes = obj.preset_minutes;

                String textViewContents = PresetDesc + " - "
                        + PresetMinutes + " minutes";

                TextView textViewItem = new TextView(this);
                textViewItem.setPadding(0, 10, 0, 10);
                textViewItem.setText(textViewContents);
                textViewItem.setTag(Long.toString(id));

                textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem());

                linearLayoutRecords.addView(textViewItem);
            }

        }

        else {

            TextView locationItem = new TextView(this);
            locationItem.setPadding(8, 8, 8, 8);
            locationItem.setText("No records yet.");

            linearLayoutRecords.addView(locationItem);
        }
    }listener

The method startActivity(Intent, int) is undefined for the type OnClickListenerSelectPresetItem


    public class SelectPresetItemActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_select_preset_item);

            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }

            readRecords();
        }

        public void readRecords() {
            LinearLayout linearLayoutRecords = (LinearLayout) findViewById(R.id.linearLayoutRecords);
            linearLayoutRecords.removeAllViews();

            List<preset_item> preset_item = new TableControllerPresetItem(this).read();

            if (preset_item.size() > 0) {

                for (preset_item obj : preset_item) {

                    long id = obj.id;
                    String PresetDesc = obj.preset_desc;
                    int PresetMinutes = obj.preset_minutes;

                    String textViewContents = PresetDesc + " - "
                            + PresetMinutes + " minutes";

                    TextView textViewItem = new TextView(this);
                    textViewItem.setPadding(0, 10, 0, 10);
                    textViewItem.setText(textViewContents);
                    textViewItem.setTag(Long.toString(id));

                    textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem());

                    linearLayoutRecords.addView(textViewItem);
                }

            }

            else {

                TextView locationItem = new TextView(this);
                locationItem.setPadding(8, 8, 8, 8);
                locationItem.setText("No records yet.");

                linearLayoutRecords.addView(locationItem);
            }
        }

This is the OnCLickListener code:

public class OnClickListenerSelectPresetItem implements OnClickListener {
    public final static String EXTRA_MEAL_ID = "com.ian.mealtimer.MEAL_ID";

    @Override
    public void onClick(View view) {

        Long selectedMealId = Long.valueOf(view.getTag().toString());
        Intent myIntent = new Intent(view.getContext(),
                MealActivity.class);
        long mealId = -1;
        myIntent.putExtra(EXTRA_MEAL_ID, selectedMealId);
        startActivity(myIntent, 0);

    }
}

You should use: startActivity (Intent intent)

there is a different method which has an integer parameter: startActivityForResult (Intent intent, int requestCode)

Also a context can start an activity, so you have to get the context. Easily you can get it from your view:

@Override
public void onClick(View view) {

    Long selectedMealId = Long.valueOf(view.getTag().toString());
    Intent myIntent = new Intent(view.getContext(),
            MealActivity.class);
    long mealId = -1;
    myIntent.putExtra(EXTRA_MEAL_ID, selectedMealId);
    view.getContext().startActivity(myIntent);
}

You can read more about starting activities here.

startActivity is a method of the class Context. The OnClickListener cannot know on which context it should be called. Pass the Context as Parameter to the constructor of the OnClickListener and save it inside: In class OnClickListenerSelectPresetItem:

private Context context;
public OnClickListenerSelectPresetItem(Context context){
   this.context=context;
}

Then call startActivity on this context

this.context.startActivity(myIntent, 0);

And construct them by passing the activity

textViewItem.setOnClickListener(new OnClickListenerSelectPresetItem(this));

How about you pass the activity to the constructor of the OnClickListenerSelectPresetItem and then use it like the following :

public class OnClickListenerSelectPresetItem implements OnClickListener {
    public final static String EXTRA_MEAL_ID = "com.ian.mealtimer.MEAL_ID";
private SelectPresetItemActivity selectPresetItemActivity;
    public OnClickListenerSelectPresetItem(SelectPresetItemActivity selectPresetItemActivity){
this.selectPresetItemActivity = selectPresetItemActivity;
     }
    @Override
    public void onClick(View view) {

        Long selectedMealId = Long.valueOf(view.getTag().toString());
        Intent myIntent = new Intent(view.getContext(),
                MealActivity.class);
        long mealId = -1;
        myIntent.putExtra(EXTRA_MEAL_ID, selectedMealId);
        selectPresetItemActivity.startActivity(myIntent, 0);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM