我正在尝试构建一个应用程序,其中员工的姓名存储在表中。在特定的页面上,用户可以在自动完成文本视图中输入员工的姓名,并选择弹出的建议之一。根据选择的内容,我想填充屏幕上的其他字段。为此,我从SQL Lite数据库读取中返回一个2d字符串数组,其中包含数组名,dept,desg等...名称数组在“自动完成”视图中提供。Android:使用在AutoCompleteTextView中选择的项目填充另一个字段
现在的问题是在onClickItemlistener方法中返回的索引。返回的索引对应于最终在单击特定项目之前显示的列表,而不是原始名称数组的那个通过了。
例如,如果我有2D阵列等:
name department designation
Abc1234 Dept1 desg1
Def1234 D2 d2
Abcxyz D3 d3
Defabc D4 D5
Abcdef D6 D6
现在,如果我在ABC公司的AutoCompleteTextView键入,仅显示3项,如果我选择ABCDEF返回的位置和ID 2,而原始数组中的索引是5.我希望这5返回某种方式,以便我可以得到D6的相应的dept和desg值..
希望我清楚了..这是我第二周的android编程。 。所以请温柔..我已经在网上搜索了足够,但无法找到这个问题的答案..
编辑:::我最终结束了创建客户适配器,但仍然有一个问题,仍然存在...我以某种方式失去了在CustomAdapter类中的按钮时按下的ArrayList对象的值..所以基本上条件“原稿尺寸()> 0”,在为performFiltering方法的循环永远不会成为成功的,并且自动完成不工作...
下面是我如何设置适配器...
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();
}
}
}
}
}
我创建了这个看到各种网站和不同的消息帖子以及数组适配器源代码...当不同的人提到相同的代码适用于他们时, 。非常感谢您的任何帮助....
所以你的意思是说而不是使用阵列适配器,我应该使用这个自定义适配器。我确实有一个像你在这里提到的人一样定义的类。让我试试这个选项,并回顾发生了什么。 – Sriman 2012-02-24 16:38:40
看起来像自定义适配器需要很多的代码来写..考虑其他解决方案(虽然不如这个优雅)... – Sriman 2012-02-24 17:38:28
我试过了自定义适配器,但有一个问题。请参阅原始文章中的编辑并帮助我...感谢您的时间... – Sriman 2012-02-25 05:36:59