我尝试创建字典,但搜索按钮不起作用。我不知道我哪里做错了

问题描述 投票:0回答:1

我正在编一本字典。但搜寻没有成功。这里我使用的是SQLite数据库。当我运行该应用程序时,没有出现错误通知。我不明白我哪里错了。其他菜单都可以,只有搜索不行。当我输入这个词时,什么也没有出现。

这是针对MainActivity.java的

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 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.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;
    
    EditText edit_search;
    DictionaryFragment dictionaryFragment;
    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);
            }
        });
    
        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){
                
            }
        });
    }
    
    @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();
        if(R.id.action_settings == id)return true;
    
        //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;
    }
}

这是我的片段字典的源代码

字典片段.java

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<>();
    
    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; } } }
    
    public void setOnFragmentListener(FragmentListener listener){
        this.listener = listener;
    }
}
java android sqlite dictionary android-sqlite
1个回答
0
投票

使用 ViewModel 从 Activity 传递数据

public class MyViewModel extends ViewModel {
private final MutableLiveData<String> realtimedata = new MutableLiveData<>();

public LiveData<String> getSensorData() {
    return realtimedata;
}

public void update(String data) {
    realtimedata.setValue(data);
}
}

观察 ViewModel WithIn Fragment 来获取 SearchData:

 private MyViewModel myViewModel;
 myViewModel = ViewModelProviders.of(this).get(MyViewModel::class.java)

 myViewModel.getSensorData().observe(viewLifecycleOwner, Observer<String> { data ->
        // Update the UI
    })
© www.soinside.com 2019 - 2024. All rights reserved.