【问题标题】:How to add multiple fields in AutoCompleteTextView in android如何在android的AutoCompleteTextView中添加多个字段
【发布时间】:2023-04-09 12:15:01
【问题描述】:

您好,我是 android 新手,在我的应用程序中我添加了 AutoCompleteTextView 好的,这很好

但这里我的主要要求是我想在 AutoCompleteTextView 中添加多个字段

像我想在我的 AutoCompleteTextView 中显示的行星、颜色、卢比字符串数组列表,如下图所示

当我点击 AutoCompleteTextView 时,数据必须显示在一个 Textview 中的任何行

我很困惑,因为我对 android 很陌生,如何做到这一点请帮助我

MainActivity:-

public class MainActivity extends AppCompatActivity {

    AutoCompleteTextView  autoCompleteTextView;

    String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
            "Jupiter", "Saturn", "Uranus", "Neptune"};

    String[] colors = new String[] { "Red", "Blue", "Green", "Yellow",
            "Orange", "Cyan", "Black", "Megenta"};

    String[] Ruppes = new String[] { "100", "200", "300", "400",
            "500", "600", "700", "800"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.AutoCompletion);
       // Conutry_Names = getResources().getStringArray(R.array.planets);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,planets);
        autoCompleteTextView.setAdapter(adapter);
    }
}

我的 xml:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical">

    <AutoCompleteTextView
        android:id="@+id/AutoCompletion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:hint="@string/Enter_Name_Here"
        />

</LinearLayout>

【问题讨论】:

    标签: android autocompletetextview


    【解决方案1】:
    @SuppressLint("NewApi")
    public class Compose_message extends Fragment {
    
        Context con;
        CustomAutoCompleteTextView to;
        public static SQLiteDatabase simpledb=null;
        public static String dbname="your database name";
        String tablename="user";
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            con=this.getActivity();
            simpledb=con.openOrCreateDatabase(dbname, con.MODE_PRIVATE, null);
            View rootView = inflater.inflate(R.layout.activity_compose_message, container, false);
           to=(CustomAutoCompleteTextView)rootView.findViewById(R.id.comose_to);
           to.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                Cursor c=simpledb.rawQuery("select id,first_name,last_name,city from '"+tablename+"' where first_name like '%"+to.getText().toString()+"%' ",null);
                        aList.clear();                 
                       if(c.getCount()>0)
                       {
                           c.moveToFirst();
                           do
                           {
                               HashMap<String, String> hm = new HashMap<String,String>();
                               hm.put("id", c.getString(c.getColumnIndex("id")));
                               hm.put("first_name", c.getString(c.getColumnIndex("first_name")));
                               hm.put("last_name", c.getString(c.getColumnIndex("last_name"))+" <");
                               hm.put("city", c.getString(c.getColumnIndex("city"))+" >");
                               aList.add(hm);
    
                           }while(c.moveToNext());
                           String[] from = {"first_name","last_name","city"};
                           int[] to1 = { R.id.first_name,R.id.last_name,R.id.city};
                           SimpleAdapter adapter = new SimpleAdapter(con, aList, R.layout.autocomplete_layout, from, to1);
                           to.setAdapter(adapter);
                       }
                       else
                       {
                       }
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
    
            }
        });
         return rootView;
    
        }
    }
    
    /*CustomAutoCompleteTextView class */
    public class CustomAutoCompleteTextView extends AutoCompleteTextView {
        String name;
        public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        /** Returns the country name corresponding to the selected item */
        @Override
        protected CharSequence convertSelectionToString(Object selectedItem) {
            /** Each item in the autocompetetextview suggestion list is a hashmap object */
            HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
    
            for(String key:hm.keySet())
            {
                 name=(String)hm.get("first_name")+" ";
                 name+=(String)hm.get("last_name")+" ";
                 name+=(String)hm.get("city");
            }
    
            return name;
        }
    }
    
    /* autocomplate layout*/
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
          android:background="@color/white"
         >
    
    
        <TextView 
                android:id="@+id/id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp" 
                android:padding="2dp"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                android:background="@color/white"           
        />  
            <TextView 
                android:id="@+id/first_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp" 
                android:padding="2dp"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                android:background="@color/white"           
        />
            <TextView 
                android:id="@+id/last_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp" 
                android:padding="2dp"
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"    
                android:background="@color/white"       
        />
            <TextView 
                android:id="@+id/city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15dp" 
                android:padding="2dp"   
                android:layout_marginTop="3dp"
                android:layout_marginBottom="3dp"
                 android:background="@color/white"        
        />
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      在构建 adapter 时,您可以根据需要组合这些值: 例如:

      String[] keywords;
      
      counter =0;
      for(i=0; i< palnets.length(); i++)
      {
        for(j=0; j< colors.length(); j++)
        {
         keywords[counter] = "Planet" + planets[i] + "Color:" + colors[j] + "Price:" + prices[j];
         counter++;
        }
      }
      
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, keywords);
          final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
          textView.setAdapter(adapter);
      

      【讨论】:

      • 这个关键字是什么?
      • 请清楚地解释包括 Xml 文件我对这个技术很陌生
      • 对不起,我忘了提到 keywords 是一个 String[] 变量。我已经相应地编辑了我的答案。我用你的例子只是为了给出答案。您可以根据需要使用类似的代码修改。将我给定的代码放入onCreate()。我认为 XML 文件不需要任何修改。
      • 自定义视图我想你可以关注另一个讨论here
      • nullPointerArray 异常来了
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2011-06-08
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多