2015-10-20 52 views
-2

我想要類型味精以及它顯示單個Edittext android中的名稱(我在我的數據庫中有什麼)的autoCompletetext。可能嗎 ?例如: -Android中的AutoCOmplete Edittext

我的名字是Amit。我住在德里。 所以在上面這句話中,Amit應該來自我的數據庫中的AutoComplete。其餘的字都是正常的。

+0

https://engineering.linkedin.com/android/open-sourcing-spyglass-flexible-library-implementing-mentions-android這一個是benificial –

回答

1

您可以使用AutoCompleteTextView

<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1" > 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:text="ID:" /> 

     <AutoCompleteTextView 
      android:id="@+id/autoID" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content"/> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:text="PW:" /> 

     <AutoCompleteTextView 
      android:id="@+id/autoPW" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:inputType="textPassword" /> 
    </TableRow> 
</TableLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/btnSubmit" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Submit" /> 

    <Button 
     android:id="@+id/btnCancel" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Cancel" /> 
</LinearLayout> 

而且活動時間:

private Button btnSubmit, btnCancel; 
private AutoCompleteTextView autoID, autoPW; 
private ListView lvID; 
private Button btnListID; 
private EditText editID; 

private ArrayList<String> arrID = new ArrayList<String>(); 
private ArrayAdapter<String> adapterID = null; 

private ArrayList<String> arrPW = new ArrayList<String>(); 
private ArrayAdapter<String> adapterPW = null; 

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

    btnSubmit = (Button) findViewById(R.id.btnSubmit); 
    btnSubmit.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      addCache(autoID.getText().toString()); 
     } 
    }); 
    btnCancel = (Button) findViewById(R.id.btnCancel); 
    btnCancel.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      autoID.setText(""); 
      autoPW.setText(""); 
     } 
    }); 
    autoID = (AutoCompleteTextView) findViewById(R.id.autoID); 
    autoPW = (AutoCompleteTextView) findViewById(R.id.autoPW); 
    getCache(); 
} 

private void getCache() { 
    SharedPreferences prefs = getSharedPreferences("LOGINS", MODE_PRIVATE); 
    int total_ID = prefs.getInt("COUNT_ID", 0); 
    while (total_ID>0) { 
     String id = prefs.getString(createKEY(total_ID), null); 
     if (id != null) { 
      arrID.add(id); 
     } 
     total_ID -=1; 
    } 
    adapterID = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, arrID); 
    autoID.setAdapter(adapterID); 
} 
private void addCache(String data) { 
    for (int i = 0; i < arrID.size(); i++) { 
     String str = arrID.get(i); 
     if (str.trim().equalsIgnoreCase(data.trim())) { 
      return; 
     } 
    } 

    arrID.add(data); 
    adapterID = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, arrID); 
    autoID.setAdapter(adapterID); 

    putIntSharedPreferences("COUNT_ID", arrID.size()); 
    putStringSharedPreferences(createKEY(arrID.size()), data); 
} 

private String createKEY(int total) { 
    String key = "ID"+total; 
    return key; 
} 
private void putIntSharedPreferences(String key, int value) { 
    SharedPreferences preferences = getSharedPreferences("LOGINS", 
      MODE_PRIVATE); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putInt(key, value); 
    editor.commit(); 
} 
private void putStringSharedPreferences(String key, String values) { 
    SharedPreferences preferences = getSharedPreferences("LOGINS", 
      MODE_PRIVATE); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.putString(key, values); 
    editor.commit(); 
} 

private String getSharedPreferences(String key) { 
    SharedPreferences preferences = getSharedPreferences("LOGINS", 
      MODE_PRIVATE); 
    return preferences.getString(key, "VALUE 1"); 
} 

我使用SharedPreferences保存ID時的onclick提交按鈕。

+0

看到問題( )之前的鏈接,然後你也會明白什麼我確實需要。 –

+0

你有試過我的代碼嗎? – ChickenVN91

+0

輸入ID並點擊提交按鈕後,ID將被保存在SharePreferences中的sugesst列表中。關於sugesst列表,您還可以爲您的請求自定義配置。 – ChickenVN91