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

时间:2012-02-24 04:19:02

标签: 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

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

希望我足够清楚..这是我编程android的第二周..所以请保持温和..我已经在网上搜索了足够多但是找不到这个问题的答案..

编辑:::我最终创建了客户端适配器,但仍有一个问题仍然存在...当按下某个键时,我在某种程度上失去了CustomAdapter类中ArrayList对象的值。所以基本上是performFiltering方法的for循环中的条件“orig.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);
        }
        });

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

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

        }

    }
}

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

2 个答案:

答案 0 :(得分:0)

您可以为AutoCompleteTextView创建一个自定义适配器,它具有所有信息,例如您有一个类People(名称,部门,名称)。

这样的东西
autoCompleteTextView.setAdapter(new CustomAdapter<People>(getBaseContext(),  android.R.layout.simple_list_item_1, "a list of all your people"))

答案 1 :(得分:0)

我发现了问题....这是因为performFiltering方法中缺少一段代码...当使用列表必须过滤的约束(或前缀)为null时,我们只需要返回整个列表返回....一旦将其添加到else中,对于约束的null检查,过滤就像黄金....

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