如何将菜单项连接到类?

时间:2018-08-23 15:49:05

标签: javascript java android jquery arrays

menuItem E-K

我上面有这个MenuItem E-K,想在其上单击以连接到名为“ DB”的片段(该DB是另一个名为“ Dictonary Fragment Java类”的片段的resetDatasource),但我不能。 这是我的代码 1-字典片段:

public class DictionaryFragment extends Fragment {
private FragmentListener listener;
ArrayAdapter<String> adapter;
ListView dictList;



public DictionaryFragment() {

}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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

    return inflater.inflate(R.layout.fragment_dictionary, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    dictList = view.findViewById(R.id.dictionaryList);
    adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, getListOfWords());
    dictList.setAdapter(adapter);

    dictList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (listener != null)
                listener.onItemClick(getListOfWords()[position]);

        }
    });
}

public void resetDataSource(String[] source) {
    adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, source);
    dictList.setAdapter(adapter);
    adapter.notifyDataSetChanged();



}



public void filterValue(String value) {
    // adapter.getFilter().filter(value);
    int size = adapter.getCount();
    for (int i = 0; i < size; i++) {
        if (adapter.getItem(i).startsWith(value)) {
            dictList.setSelection(i);
            break;
        }
    }
}

String[] getListOfWords() {
    String[] source = new String[]{
            "a"
            , "a, an"
            , "aback"
            , "abacus"
            , "abaft"
            , "abalone"
            , "abandon"
            , "abandoned"
            , "abandonment"
            , "abandoning"
            , "abase"
            , "abasement"
            , "abash"
            , "abashed"
            , "abate"
            , "abatement"
            , "abattoir"
            , "abbess"
            , "abbey"
            , "abbot"
            , "abbrev"
            , "abbreviate"
            , "abbreviated"
            , "abbreviation"

    };
    return source;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onDetach() {
    super.onDetach();

}

public void setOnFragmentListener(FragmentListener listener) {
    this.listener = listener;
}

}

2-DB:

public class DB {

public static String[] getData(int id) {
    if (id == R.id.action_eng_kh) {
        return getEngKh();
    }
    return new String[0];
}

private static String[] getEngKh() {
    String[] source = new String[]{
            "a"
            , "a, an"
            , "aback"
            , "abacus"
            , "abaft"
            , "abalone"
            , "abandon"
            , "abandoned"
            , "abandonment"
            , "abandoning"
            , "abase"
            , "abasement"
            , "abash"
            , "abashed"
            , "abate"
            , "abatement"
            , "abattoir"
            , "abbess"
            , "abbey"
            , "abbot"
            , "abbrev"
            , "abbreviate"
            , "abbreviated"
            , "abbreviation"

    };
    return source;
    }

    }

3-In MainActivity

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
MenuItem menuSetting;
DictionaryFragment dictionaryFragment;
BookmarkFragment bookmarkFragment;
DetailFragment detailFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    dictionaryFragment = new DictionaryFragment();

    bookmarkFragment = new BookmarkFragment();


    goToFragment(dictionaryFragment, true);

    dictionaryFragment.setOnFragmentListener(new FragmentListener() {
        @Override
        public void onItemClick(String value) {

            goToFragment(DetailFragment.getNewInstance(value), false);
        }
    });
    bookmarkFragment.setOnFragmentListener(new FragmentListener() {
        @Override
        public void onItemClick(String value) {

            goToFragment(DetailFragment.getNewInstance(value), false);
        }
    });

    EditText edit_search = findViewById(R.id.edit_search);
    edit_search.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
           dictionaryFragment.filterValue(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    menuSetting = menu.findItem(R.id.action_settings);
    String id = Global.getState(this, "dic_type");
    if(id!= null)
        onOptionsItemSelected(menu.findItem(Integer.valueOf(id)));
    else
        dictionaryFragment.resetDataSource(DB.getData(R.id.action_eng_kh));
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    Global.saveState(this, "dic_type", String.valueOf(id));



    if (id == R.id.action_eng_kh) {

      dictionaryFragment.resetDataSource(DB.getData(id));

        menuSetting.setIcon(getDrawable(R.drawable.ic_ek));


    }

    return super.onOptionsItemSelected(item);
}




@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_bookmark) {
    goToFragment(bookmarkFragment, false);


    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
void goToFragment(Fragment fragment, boolean isTop){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.replace(R.id.fragment_container, fragment);
    if(!isTop)
        fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

}

4- main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:icon="@drawable/ic_seldic"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="always">
    <menu>
        <item
            android:id="@+id/action_eng_kh"
            android:icon="@drawable/ic_ek"
            android:orderInCategory="100"
            android:title="English-Khmer"
            app:showAsAction="never" />

    </menu>
</item>

请帮助我,因为我是代码新手,在Stackoverflow中我需要大家的帮助 谢谢

0 个答案:

没有答案