[英]How do I implement SearchView with no errors
我是初學者和編碼新手。 我在 Android Studio 中遇到了 ListView 的問題。 我用一個簡單的 Listview 創建了一個簡單的活動。 Listview 包含位置,當用戶單擊某個項目時,應用程序將打開谷歌地圖並將用戶帶到該位置。 當我實現 SearchView 時出現問題。 應用搜索時,無論過濾結果如何,它將始終打開第一個位置。 所以你能幫我解決這個問題嗎? 謝謝。 這是我的代碼,很抱歉造成混亂。
MainActivity.java
import com.example.myapplicationsecond.R;
public class MainActivity9 extends AppCompatActivity {
ListView listView;
String[] name = {"First Location","Second Location","Third Location","Fourth Location",};
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main9);
View view = getLayoutInflater().inflate(R.layout.abs_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER);
TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
Title.setText("Search Here");
getSupportActionBar().setCustomView(view,params);
getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
getSupportActionBar().setTitle("Search Here");
listView = findViewById(R.id.listview);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,name);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 21.422458, 39.826213"));
startActivity(intent);
}
if(position==1){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 24.467275, 39.610629"));
startActivity(intent);
}
if(position==2){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 25.173059, 45.142079"));
startActivity(intent);
}
if(position==3){
Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("geo: 26.348400, 43.766664"));
startActivity(intent);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search Here");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
arrayAdapter.getFilter().filter(newText);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
}
MainActivity.Xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity9">
<ListView
android:id="@+id/listview"
android:textDirection="locale"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
這就是你編碼它的方式,你有if(position==0)
所以無論第一個位置是什么,你都會打開相同的geo:
。 你應該檢查點擊時第一個位置是什么,所以在onItemClick
里面放:
String clickedText = arrayAdapter.getItem(position);
然后在所有項目數組中找到該項目的位置
int positionInArray = java.util.Arrays.asList(name).indexOf(clickedText);
現在將positionInArray
用於您的is
else
但這是一個快速修復,你應該有一些模型,你的自定義類有兩個變量, String name
和String geoUri
或兩個long
s 用於 lat 和 lng
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.