My fragment details can’t be opened. when I type a word in search and I want to see the details of the word it won’t open. I don’t know where the error lies. Please help me. I’m still new to Android Studio
detail fragment
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;
public class DetailFragment extends Fragment {
private String value = "";
private TextView tvWord;
private ImageButton btnBookmark;
private WebView tvWordTranslate;
private DBHelper mDBHelper;
private int mDicType;
public DetailFragment() {
// Required empty public constructor
}
public static DetailFragment getNewInstance(String value, DBHelper dbHelper, int dicType){
DetailFragment fragment = new DetailFragment();
fragment.value = value;
fragment.mDBHelper = dbHelper;
fragment.mDicType = dicType;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_detia, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
tvWord = (TextView) view.findViewById(R.id.tvWord);
tvWordTranslate = (WebView) view.findViewById(R.id.tvWordTranslate);
btnBookmark = (ImageButton) view.findViewById(R.id.btnBookmark);
final Word word = mDBHelper.getWord(value, mDicType);
tvWord.setText(word.key);
tvWordTranslate.loadDataWithBaseURL(null, word.value,"text/html", "utf-8",null);
Word bookmarkWord = mDBHelper.getWordFromBookmark(value);
int isMark = bookmarkWord == null ? 0 : 1;
btnBookmark.setTag(isMark);
int icon = bookmarkWord == null ? R.drawable.ic_bookmark_border : R.drawable.ic_bookmark_fill;
btnBookmark.setImageResource(icon);
btnBookmark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int i = (int) btnBookmark.getTag();
if (i == 0) {
btnBookmark.setImageResource(R.drawable.ic_bookmark_fill);
btnBookmark.setTag(1);
mDBHelper.addBookmark(word);
} else if (i == 1) {
btnBookmark.setImageResource(R.drawable.ic_bookmark_border);
btnBookmark.setTag(0);
mDBHelper.removeBookmark(word);
}
}
});
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
}
I’m trying to make a dictionary.
It’s just that the word details don’t work, I don’t know where the error is
dictionary fragment
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class DictionaryFragment extends Fragment {
private String value = "Hello everyone!!!";
private FragmentListener listener;
ArrayAdapter<String> adapter;
ListView dicList;
private ArrayList<String> mSource = new ArrayList<String>();
public DictionaryFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dictionary, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
dicList = view.findViewById(R.id.dictionaryList);
adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,mSource);
dicList.setAdapter(adapter);
dicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (listener != null)
listener.onItemClick(mSource.get(position));
}
});
}
public void resetDataSource(ArrayList<String> source){
mSource = source;
adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,mSource);
dicList.setAdapter(adapter);
}
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)){
dicList.setSelection(i);
break;
}
}
}
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
public void setOnFragmentListener(FragmentListener listener){
this.listener = listener;
}
}
main activity
import androidx.appcompat.app.ActionBarDrawerToggle; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.core.view.GravityCompat; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.ViewModel;
import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; import android.widget.EditText;
import com.google.android.material.navigation.NavigationView;
import java.util.ArrayList;
public class kamusActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
MenuItem menuSetting;
Toolbar toolbar;
DBHelper dbHelper;
DictionaryFragment dictionaryFragment;
DetailFragment detailFragment;
BookmarkFragment bookmarkFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kamus);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbHelper = new DBHelper(this);
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((NavigationView.OnNavigationItemSelectedListener) this);
dictionaryFragment = new DictionaryFragment();
bookmarkFragment = BookmarkFragment.getNewInstance(dbHelper);
goToFragment(dictionaryFragment, true);
dictionaryFragment.setOnFragmentListener(new FragmentListener() {
@Override
public void onItemClick(String value) {
String id = Bahasa.getState(kamusActivity.this,"dic_type");
int dicType = id == null? R.id.action_it:Integer.valueOf(id);
goToFragment( DetailFragment.getNewInstance(value,dbHelper,dicType),false);
}
});
bookmarkFragment.setOnFragmentListener(new FragmentListener() {
@Override
public void onItemClick(String value) {
String id = Bahasa.getState(kamusActivity.this,"dic_type");
int dicType = id == null? R.id.action_it:Integer.valueOf(id);
goToFragment( DetailFragment.getNewInstance(value,dbHelper,dicType),false);
}
});
EditText edit_search = findViewById(R.id.edit_search);
edit_search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2){
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
dictionaryFragment.filterValue(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable){
}
});
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
@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) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present
getMenuInflater().inflate(R.menu.main_menu, menu);
menuSetting = (MenuItem) menu.findItem(R.id.action_settings);
String id = Bahasa.getState(this,"dic_type");
if (id != null)
onOptionsItemSelected(menu.findItem(Integer.valueOf(id)));
else {
ArrayList<String> source = dbHelper.getWord(R.id.action_it);
dictionaryFragment.resetDataSource(source);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_it) {
Bahasa.saveState(this,"dic_type", String.valueOf(id));
ArrayList<String> source = dbHelper.getWord(id);
dictionaryFragment.resetDataSource(source);
menuSetting.setIcon(getDrawable(R.drawable.icon_bibt_2));
return true;
}else if (id == R.id.action_ti){
Bahasa.saveState(this,"dic_type", String.valueOf(id));
ArrayList<String> source = dbHelper.getWord(id);
dictionaryFragment.resetDataSource(source);
menuSetting.setIcon(getDrawable(R.drawable.icon_btbi_2));
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_bookmark){
String activeFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container).getClass().getSimpleName();
if(!activeFragment.equals(BookmarkFragment.class.getSimpleName())){
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();
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
String activeFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container).getClass().getSimpleName();
if (activeFragment.equals(BookmarkFragment.class.getSimpleName())){
menuSetting.setVisible(false);
toolbar.findViewById(R.id.edit_search).setVisibility(View.GONE);
toolbar.setTitle("Bookmark");
}else {
menuSetting.setVisible(true);
toolbar.findViewById(R.id.edit_search).setVisibility(View.VISIBLE);
toolbar.setTitle("");
}
return true;
}
}`