Android: using the item selected in AutoCompleteTextView to populate another field

I am trying to create an application in which employee names are stored in a table. On a specific page, the user can enter the employeeโ€™s name in the autocompletetextview field and select one of the tooltips. Based on what was selected, I want to fill in other fields on the screen. To do this, I return a 2d string array from the SQL Lite database containing the array name, dept, desg, etc. An array of names feeds in the Auto Complete view.

Now the problem is with the index returned in the onClickItemlistener method. The returned index corresponds to the list that was finally displayed before the particular item was selected, and not the one that was from the original array of names that passed.

For example, if I have a 2d array, for example:

name department designation Abc1234 Dept1 desg1 Def1234 D2 d2 Abcxyz D3 d3 Defabc D4 D5 Abcdef D6 D6 

Now, if I type Abc in in AutoCompleteTextView, only 3 elements are displayed, and if I select Abcdef, then the position and returned id will be 2, while the index in the original array is 5. I want this 5 to be returned anyway, so that I can get the corresponding dept and desg values โ€‹โ€‹for D6 ..

I hope I'm clear enough. This is my second week of android programming. Therefore, please be careful. I have already searched the Internet enough, but could not find the answer to this question.

EDIT: I finally finished creating the client adapter, but there is still one problem that persists ... I somehow lose the value of the ArrayList object in the CustomAdapter class when I press a key. the condition "orig.size ()> 0" in the for loop of the performFiltering method never succeeds and autocomplete does not work ...

The following describes how I install the adapter ...

  ArrayList<Part_Mstr_Info> mAllParts = partMstrDbHelper.getAll(); if (mAllParts != null) { /* ac_part_id = mAllParts.get_part_id(); ac_name = mAllParts.get_name(); ac_desg = mAllParts.get_desg(); ac_org = mAllParts.get_org(); ac_dept = mAllParts.get_dept();*/ adapter = new CustomAdapter(this, R.layout.ac_name_list, mAllParts); mName.setAdapter(adapter); mName.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> adapter, View view, int index, long id) { Part_Mstr_Info part_mstr_info = (Part_Mstr_Info) adapter.getItemAtPosition(index); mPartMstrID = part_mstr_info.get_part_id(); name = part_mstr_info.get_name(); mName.setText(name); desg = part_mstr_info.get_desg(); mDesg.setText(desg); org = part_mstr_info.get_org(); mOrg.setText(org); dept = part_mstr_info.get_dept(); mDept.setText(dept); } }); 

The following describes my custom adapter ....

 package com.meeting.minutes; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.TextView; import com.meeting.minutes.PartMstrDbAdapater.Part_Mstr_Info; public class CustomAdapter extends ArrayAdapter<Part_Mstr_Info> implements Filterable{ private ArrayList<Part_Mstr_Info> entries, orig; private Activity activity; private ArrayFilter myFilter; public CustomAdapter(Activity a, int textViewResourceId, ArrayList<Part_Mstr_Info> entries) { super(a, textViewResourceId, entries); this.entries = entries; orig = this.entries; this.activity = a; } public static class ViewHolder{ public TextView tv_ac_name; public TextView tv_ac_desg; public TextView tv_ac_org; public TextView tv_ac_dept; } @Override public int getCount(){ return entries!=null ? entries.size() : 0; } @Override public Part_Mstr_Info getItem(int index) { return entries.get(index); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewHolder holder; if (v == null) { LayoutInflater vi = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = vi.inflate(R.layout.ac_name_list, null); holder = new ViewHolder(); holder.tv_ac_name = (TextView) v.findViewById(R.id.ac_name); holder.tv_ac_desg = (TextView) v.findViewById(R.id.ac_desg); holder.tv_ac_org = (TextView) v.findViewById(R.id.ac_org); holder.tv_ac_dept = (TextView) v.findViewById(R.id.ac_dept); v.setTag(holder); } else holder=(ViewHolder)v.getTag(); final Part_Mstr_Info custom = entries.get(position); if (custom != null) { holder.tv_ac_name.setText(custom.get_name()); holder.tv_ac_desg.setText(custom.get_desg()); holder.tv_ac_org.setText(custom.get_org()); holder.tv_ac_dept.setText(custom.get_dept()); } return v; } @Override public Filter getFilter() { if (myFilter == null){ myFilter = new ArrayFilter(); } return myFilter; } private class ArrayFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { if (orig == null) orig = entries; if (constraint != null) { ArrayList<Part_Mstr_Info> resultsSuggestions = new ArrayList<Part_Mstr_Info>(); for (int i = 0; i < orig.size(); i++) { if(orig.get(i).get_name().toLowerCase().startsWith(constraint.toString().toLowerCase())){ resultsSuggestions.add(orig.get(i)); } } FilterResults results = new FilterResults(); results.values = resultsSuggestions; results.count = resultsSuggestions.size(); return results; } else { return new FilterResults(); } } @Override @SuppressWarnings("unchecked") protected void publishResults(CharSequence constraint, FilterResults results) { clear(); ArrayList<Part_Mstr_Info> newValues = (ArrayList<Part_Mstr_Info>) results.values; if(newValues !=null) { for (int i = 0; i < newValues.size(); i++) { add(newValues.get(i)); } if(results.count>0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } } } } 

I created this by seeing various websites and different messages, as well as the source code of the Array Adapter ... I canโ€™t understand where everything goes wrong when several other people mentioned that the same code works for them .... Any help is much appreciated ....

+5
source share
2 answers

You can create a custom adapter for your AutoFill TextView that has all the information, say you have a People class (name, department, designation).

Sort of

 autoCompleteTextView.setAdapter(new CustomAdapter<People>(getBaseContext(), android.R.layout.simple_list_item_1, "a list of all your people")) 
0
source

I found a problem ... This was due to a missing piece of code in the executeFiltering method ... When the restriction (or prefix) by which the list should be filtered is null, we just need to return the whole list back .... How only this was added to else for a zero constraint check, filtering works like gold ....

  ArrayList <Part_Mstr_Info> list = new ArrayList <Part_Mstr_Info>(orig); results.values = list; results.count = list.size(); 
0
source

Source: https://habr.com/ru/post/1398163/


All Articles