Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:05:44+00:00 2026-06-17T13:05:44+00:00

I’m trying to convert Activity to a fragment and can’t override this methods :

  • 0

I’m trying to convert Activity to a fragment and can’t override this methods : onCreateOptionsMenu,onOptionsItemSelected,onContextItemSelected.
,maybe some import statements are missing ? don’t know what to do.Her is my Class file :
package com.wts.ui;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class MainFragment extends Fragment {

    protected final static int REQUEST_CODE = 1;

    public static WordsDBAdapter dbAdapter;
    private CustomAdapter cDataAdapter;
    private Button button;
    private EditText editWord;
    private EditText editTranslate;
    private ListView listView;
    private String selectedWord;
    private Cursor cursor;

    // context menu
    private final static int IDM_EDIT = 101;
    private final static int IDM_DELETE = 102;
    private final static int IDM_INFO = 103;

    // options menu
    private static final int IDM_ABOUT = 201;
    private static final int IDM_EXIT = 202;
    private static final int IDM_SETTINGS = 203;
    private static final int IDM_QUESTION = 204;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        dbAdapter = new WordsDBAdapter(getActivity());
        dbAdapter.open();

        displayListView();
        registerForContextMenu(listView);

        // ================ListView onLongClick========================
        listView.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                cursor = (Cursor) listView.getItemAtPosition(arg2);
                selectedWord = cursor.getString(WordsDBAdapter.ID_COL);
                return false;
            }
        });

        // ================Button onClick========================
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String word = editWord.getText().toString();
                String translate = editTranslate.getText().toString();

                if (word.length() > 0 && translate.length() >= 0) {
                    Cursor cursor = dbAdapter.fetchWordsByName(word);// chek is
                                                                        // word
                                                                        // repeat

                    if (cursor.moveToFirst()) {
                        Toast.makeText(getActivity().getApplicationContext(),
                                getResources().getString(R.string.word_exist),
                                Toast.LENGTH_SHORT).show();
                    } else if (!CheckWordInput(word)
                            || !CheckTranslateInput(translate)) {
                        Toast.makeText(
                                getActivity().getApplicationContext(),
                                getResources().getString(
                                        R.string.incorrect_input),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        dbAdapter.insertWord(word, translate, " ",
                                String.valueOf(false), 0, 0, new Date());
                        displayListView();

                        editWord.setText("");
                        editTranslate.setText("");
                    }
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_main, container, false);

        button = (Button) view.findViewById(R.id.buttonAddWord);
        listView = (ListView) view.findViewById(R.id.listWords);
        editWord = (EditText) view.findViewById(R.id.editWord);
        editTranslate = (EditText) view.findViewById(R.id.editTranslate);

        return view;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        // setContentView(R.layout.activity_main);

    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        if (v.getId() == R.id.listWords) {
            String[] menuItems = getResources().getStringArray(
                    R.array.contextMenuItems);

            menu.add(Menu.NONE, IDM_EDIT, Menu.NONE,
                    menuItems[StartActivity.CONTEXT_MENU_EDIT]);
            menu.add(Menu.NONE, IDM_INFO, Menu.NONE,
                    menuItems[StartActivity.CONTEXT_MENU_INFO]);
            menu.add(Menu.NONE, IDM_DELETE, Menu.NONE,
                    menuItems[StartActivity.CONTEXT_MENU_DELETE]);

        }
    }

    //
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case IDM_EDIT: {
            Intent intent = new Intent(getActivity(), EditActivity.class);
            intent.putExtra(getResources().getString(R.string.fstRow),
                    cursor.getString(WordsDBAdapter.WORD_COL));
            intent.putExtra(getResources().getString(R.string.scndRow),
                    cursor.getString(WordsDBAdapter.TRANS_COL));
            intent.putExtra(getResources().getString(R.string.thrdRow),
                    cursor.getString(WordsDBAdapter.DESC_COL));

            startActivityForResult(intent, REQUEST_CODE);
        }
            break;
        case IDM_DELETE:
            dbAdapter.deleteWord(selectedWord);
            displayListView();
            break;
        case IDM_INFO: {
            Intent intent = new Intent(getActivity(), InformationActivity.class);
            for (int i = 1; i <= InformationActivity.nListItems; i++)
                intent.putExtra(String.valueOf(i), cursor.getString(i));

            startActivity(intent);
        }
            break;
        default:
            return super.onContextItemSelected(item);

        }
        return true;
    }

    private void displayListView() {
        // Cursor cursor = dbAdapter.fetchAllTranslated();
        Cursor cursor = dbAdapter.fetchAllTranslated();

        String[] columns = new String[] { WordsDBAdapter.KEY_WORD,
                WordsDBAdapter.KEY_TRANSLATION, WordsDBAdapter.KEY_SUCCEEDED, };

        int[] to = new int[] { R.id.textViewTranslate, R.id.textViewWord,
                R.id.textViewSuccessPoints };

        cDataAdapter = new CustomAdapter(getActivity(), R.layout.word_info,
                cursor, columns, to);

        listView.setAdapter(cDataAdapter);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.activity_main, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case IDM_ABOUT: {
            Intent intent = new Intent(getActivity(), AboutActivity.class);
            startActivity(intent);
            break;
        }
        case IDM_EXIT: {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getActivity().finish();
            break;
        }
        case IDM_SETTINGS: {
            Intent intent = new Intent(getActivity(), SettingsActivity.class);
            startActivity(intent);
            break;
        }
        case IDM_QUESTION: {
            if (!StartActivity.isMainActivitySart)
                getActivity().onBackPressed();
            else {
                Intent intent = new Intent(getActivity(),
                        QuestionActivity.class);
                startActivity(intent);
            }
        }
            break;
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE) {
            if (intent.hasExtra(getResources().getString(R.string.fstRow))) {
                dbAdapter.changeValue(
                        selectedWord,
                        intent.getExtras().getString(
                                getResources().getString(R.string.fstRow)),
                        intent.getExtras().getString(
                                getResources().getString(R.string.scndRow)),
                        intent.getExtras().getString(
                                getResources().getString(R.string.thrdRow)),
                        null, null, null, null);
                displayListView();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        SettingsManager.setPreferedLanguage(getActivity());// set language
        displayListView();
    }

    public static boolean CheckTranslateInput(String str) {
        Pattern inputPattern = Pattern.compile("[\\p{L} -]{0,25}");
        Matcher inputMatcher = inputPattern.matcher(str);
        return inputMatcher.matches();
    }

    public static boolean CheckWordInput(String str) {
        Pattern inputPattern = Pattern.compile("[\\p{L} -]{1,25}");
        Matcher inputMatcher = inputPattern.matcher(str);
        return inputMatcher.matches();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        dbAdapter.close();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:05:45+00:00Added an answer on June 17, 2026 at 1:05 pm

    You have to think about what you want your Fragment to be – Fragment or SherlockFragment?

    You import this:

    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuInflater;
    import com.actionbarsherlock.view.MenuItem;
    

    And so, your overrides aren’t overrides because Menu & friends are different classes right now than what Fragment needs – they are ActionBarSherlock’s classes.

    If you extend SherlockFragment instead, your overrides should work, and it is recommended to extend SherlockFragment if you are using ActionBarSherlock (which based on your tags, you are).

    If you want to keep this as a regular fragment, then import:

    android.view.Menu
    android.view.MenuItem
    android.view.MenuInflater
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.