im new to android development and i have managed to develop an application with a listview of name, phone number and district, which is searchable and it is working really well. im using positions to click and take me to the dial pad, but the problem is, if an item is searched, it does not mantain its position given to it initially. what i want when a user searches for a name or district, it opens in the dial pad with a number written there. Here is my contacts.java
public class Contacts extends AppCompatActivity {
ListView listView;
String[] name = {"Achibu john peter - Serere - 0772698033", "Agaba - Amon - Rukiga -
0782090694\n", "Agaja joseph - kaberamaido - 0775004193/0752451894\n",}
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("DFO Contacts");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
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<?> adapterView, View view, int position, long l) {
if (position == 0){
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel: +256772698033"));
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 for your DFO");
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);
and menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search_view"
android:title="Search"
app:showAsAction="always"
android:icon="@drawable/ic_search"
app:actionViewClass="android.widget.SearchView"
/>
</menu>
And the activity_contacts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Contacts">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#D4AC0D"
app:subtitleTextColor="#0A0A0A"
app:titleTextColor="#111111"
/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="@font/poppinsmedium"
android:textSize="20sp"
android:layout_marginLeft="15dp"
android:clickable="true"
/>
</LinearLayout>