Android:使用在AutoCompleteTextView中选择的项目填充另一个字段 [英] Android: Using item selected in AutoCompleteTextView to populate another field

查看:101
本文介绍了Android:使用在AutoCompleteTextView中选择的项目填充另一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在问题出在onClickItemlistener方法中返回的索引.返回的索引对应于在单击特定项目之前最终显示的列表,而不是原始名称数组的列表.通过.

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.

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

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

现在,如果我在AutoCompleteTextView中键入Abc,则仅显示3个项目,如果我选择Abcdef,则返回的位置和id为2,而原始数组中的索引为5.我希望此5以某种方式返回,以便我可以获取D6的对应的dept和desg值.

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..

希望我足够清楚..这是我第二周进行Android编程..所以请保持温柔..我已经在网上搜索了足够的内容,但找不到此问题的答案..

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..

::我终于结束了创建客户适配器的工作,但是仍然存在一个问题……当按下一个键时,我就以某种方式失去了CustomAdapter类中ArrayList对象的值. performFiltering方法的for循环中的条件"orig.size()> 0"永远不会成功,并且自动完成功能不起作用...

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

        }

    }
}

我创建此文件时会看到各种各样的网站和不同的消息以及Array Adapter源代码...当其他几个人提到相同的代码对他们有用时,Cant会理解我哪里出了问题. .任何帮助都将不胜感激....

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....

推荐答案

您可以为AutoCompleteTextView创建一个自定义适配器,其中包含所有信息,例如您有一个类People(名称,部门,名称).

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

类似

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

这篇关于Android:使用在AutoCompleteTextView中选择的项目填充另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆