Android:使用AutoCompleteTextView中选择的项来填充另一个字段

Sri*_*man 5 android position autocompletetextview

我正在尝试构建一个应用程序,其中员工的姓名存储在表中.在特定页面上,用户可以在autocompletetextview中输入员工的姓名,并选择其中一个弹出的建议.根据选择的内容,我想在屏幕上填充其他字段.为此,我从SQL Lite数据库读取返回一个2d字符串数组,其中包含数组名称,dept,desg等...名称数组在"自动完成"视图中提供.

现在的问题是,与在onClickItemlistener方法返回的指标..这正返回对应于最终显示被点击之前的特定项目,而不是说这是通过原来的名称数组列表索引.

例如,如果我有像2d数组:

    name            department             designation
   Abc1234            Dept1                   desg1
   Def1234            D2                       d2
   Abcxyz             D3                        d3
   Defabc             D4                       D5
   Abcdef             D6                       D6
Run Code Online (Sandbox Code Playgroud)

现在,如果我输入Abc in AutoCompleteTextView,只显示3个项目,如果我选择Abcdef,返回的位置和id是2,而原始数组中的索引是5.我希望这个5以某种方式返回,以便我可以得到相应的D6的dept和desg值..

希望我足够清楚..这是我的第二周安卓编程..所以请温柔..我已经在网上搜索足够但无法找到这个问题的答案..

编辑:::我最终创建了客户端适配器,但仍有一个问题仍然存在...当按下某个键时,我在某种程度上丢失了CustomAdapter类中ArrayList对象的值.所以基本上条件为"orig" performFiltering方法的for循环中的.size()> 0"永远不会成功,自动完成功能不起作用......

以下是我设置适配器的方法......

      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);
        }
        });
Run Code Online (Sandbox Code Playgroud)

以下是我的自定义适配器的编写方式....

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();
                }   
            }    

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我创建了这个看到各种网站和不同的消息帖子以及Array Adapter源代码...当其他几个人提到相同的代码适合他们时,不能理解我出错的地方.....任何帮助非常感谢....

Bas*_*aix 1

您可以为 AutoCompleteTextView 创建一个自定义适配器,其中包含所有信息,假设您有一个 People 类(姓名、部门、​​职务)。

就像是

autoCompleteTextView.setAdapter(new CustomAdapter<People>(getBaseContext(),  android.R.layout.simple_list_item_1, "a list of all your people"))
Run Code Online (Sandbox Code Playgroud)