Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Using item selected in AutoCompleteTextView to populate another field

I am trying to build an app where the names of employees are stored in a table. On a particular page, the user can enter the name of an employee in an autocompletetextview and select one of the suggestions that pop-up. Based on what was selected, I want to populate other fields on the screen. For this I am returning a 2d string array from the SQL Lite database read containing the arrays name, dept, desg etc... The name array feeds in the Auto Complete view.

Now the issue is with the index that is being returned in the onClickItemlistener method.. The index that is being returned corresponds to the list that was displayed finally before a particular item was clicked and not that of the original name array that was passed.

For example if I have the 2d array like:

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

Now if I type in Abc in to the AutoCompleteTextView, only 3 items are displayed and if I select Abcdef the position and id returned are 2 whereas the index in original array is 5. I want this 5 to returned somehow so that I can get the corresponding dept and desg values of D6..

Hope I am clear enough.. This is my 2nd week of android programming.. so please be gentle.. I have already searched the web enough but couldn't find an answer to this question..

EDIT::: I finally ended up creating the Customer Adapter but there is still one issue that persists... I somehow am loosing the value of the ArrayList object in the CustomAdapter class when a key is pressed.. So essentially the condition "orig.size() > 0" in the for loop of the performFiltering method never becomes successful and the autocomplete doesnt work...

Below is how I am setting 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);
        }
        });

Below is how my Custom Adapter is written....

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 seeing various websites and different message posts as well as the Array Adapter source code... Cant understand where things are going wrong for me when several other people have mentioned that the same code works for them..... Any help is greatly appreciated....

like image 787
Sriman Avatar asked Nov 25 '22 03:11

Sriman


1 Answers

You can create a custom Adapter for your AutoCompleteTextView which has all the info, say you have a class People(name, department, designation).

Something like

autoCompleteTextView.setAdapter(new CustomAdapter<People>(getBaseContext(),  android.R.layout.simple_list_item_1, "a list of all your people"))
like image 61
Bastaix Avatar answered Dec 17 '22 03:12

Bastaix