我正在制作字典应用程序。在我的应用中,我正在使用此代码进行搜索
public void filterValue(String value){
int size = adapter.getCount();
for(int i = 0; i<size;i++){
if (adapter.getItem(i).startsWith(value)){
dicList.setSelection(i);
break;
}
}
}
但这不是一个好消息,它将结果放在单词列表的顶部,但是我仍然可以在列表上看到不相关的单词。您认为有更好的搜索版本吗?
我以前使用过另一个版本
adapter.getFilter().filter(value);
当我使用此代码时,它会完美列出(只是我在搜索栏中写的单词),但是当我单击某个结果时,它会指引我另一个单词。例如,我单击-abc-,但显示-acc-详细信息。
所以任何人对此都有建议。谢谢。
****编辑****
DictionaryFragment
import android.content.Context;
import android.net.Uri;
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.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class DictionaryFragment extends Fragment {
private int top;
private int index;
private FragmentListener listener;
ArrayAdapter<String> adapter;
ListView dicList;
private Adapter mAdapter;
private ArrayList<String> mSource= new ArrayList<String>();
public DictionaryFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@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(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
dicList = view.findViewById(R.id.dictionaryList);
adapter = new ArrayAdapter<String>(getContext(),R.layout.kelimelistesi, 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));
}
});
}
//bundan sonra ikinci sözlük olayı başladı
public void resetDatasource(ArrayList<String> source) {
adapter.clear();
adapter.addAll(source);
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)){
dicList.setSelection(i);
break;
}
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach(); }
public void setOnFragmentListener(FragmentListener listener){
this.listener= listener;
}
}
MainActivity
import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.WindowManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import androidx.annotation.RequiresApi;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.Menu;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
MenuItem menuSetting;
Toolbar toolbar;
DBHelper dbHelper;
DictionaryFragment dictionaryFragment;
BookmarkFragment bookmarkFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbHelper = new DBHelper(this);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
dictionaryFragment = new DictionaryFragment();
bookmarkFragment = BookmarkFragment.getInstance(dbHelper);
goToFragment(dictionaryFragment, true);
dictionaryFragment.setOnFragmentListener(new FragmentListener() {
@Override
public void onItemClick(String value) {
String id = Global.getState(MainActivity.this,"dict_type");
int dicType = id == null? R.id.eng_kh:Integer.valueOf(id);
goToFragment(DetailFragment.getNewInstance(value, dbHelper, dicType), false);
}
});
bookmarkFragment.setOnFragmentListener(new FragmentListener() {
@Override
public void onItemClick(String value) {
String id = Global.getState(MainActivity.this,"dict_type");
int dicType = id == null? R.id.eng_kh: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;
drawer = this.<DrawerLayout>findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@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);
menuSetting = menu.findItem(R.id.action_settings);
String id = Global.getState(this,"dict_type");
if (id != null)
onOptionsItemSelected(menu.findItem(Integer.valueOf(id)));
else {
ArrayList<String> source =dbHelper.getWord(R.id.eng_kh);
dictionaryFragment.resetDatasource(source);
}
return true;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.eng_kh) {
Global.saveState(this, "dict_type", String.valueOf(id));
ArrayList<String> source = dbHelper.getWord(id);
dictionaryFragment.resetDatasource(source);
return true;
} else if (id==R.id.kh_eng){
Global.saveState(this, "dict_type", String.valueOf(id));
ArrayList<String> source = dbHelper.getWord(id);
dictionaryFragment.resetDatasource(source);
return true;
}
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){
String activeFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container).getClass().getSimpleName();
if (!activeFragment.equals(BookmarkFragment.class.getSimpleName())){
goToFragment(bookmarkFragment, false);
}
}
if (id == R.id.nav_dict){
String activeFragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container).getClass().getSimpleName();
if (!activeFragment.equals(DictionaryFragment.class.getSimpleName())){
goToFragment(dictionaryFragment, false);
}
}
if (id == R.id.nav_share){
String shareBody = "Here is the share content body";
Intent shareintent = new Intent();
shareintent.setAction(Intent.ACTION_SEND);
shareintent.putExtra(Intent.EXTRA_SUBJECT, shareBody);
shareintent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.apkyolu));
shareintent.setType("text/plain");
startActivity(Intent.createChooser(shareintent, "Paylaş!!!"));
}
if (id == R.id.nav_mail){
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("message/rfc822");
i.setData(Uri.parse("mailto:"));
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"test@gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
if (id == R.id.nav_rate){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=PackageName")));
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
///////////////deneme dict ///////
////////////////deneme dict /////////////
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(DetailFragment.class.getSimpleName())){
menuSetting.setVisible(false);
toolbar.findViewById(R.id.edit_search).setVisibility(View.GONE);
toolbar.setTitle(getResources().getString(R.string.app_name));
} else if (activeFragment.equals(BookmarkFragment.class.getSimpleName())) {
menuSetting.setVisible(false);
toolbar.findViewById(R.id.edit_search).setVisibility(View.GONE);
toolbar.setTitle(getResources().getString(R.string.bookmark));
}else {
menuSetting.setVisible(true);
toolbar.findViewById(R.id.edit_search).setVisibility(View.VISIBLE);
toolbar.setTitle("");
}
return true;
}
}
Word.java
public class Word {
public String key = "";
public String value = "";
public Word (){
}
public Word(String key, String value ) {
this.key = key;
this.value = value;
}
}
答案 0 :(得分:0)
由于物品位置不一致,您将获得不同的物品。从列表中添加/删除项目时,项目位置会发生变化。
过滤代码
adapter.getFilter().filter(value);
setOnItemClickListener
dicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
if (listener!=null)
listener.onItemClick(adapterView.getAdapter().getItem(position));
}
});
从mSource
更改为adapterView
,我们从那里检索给定点击位置上的项目。