我想了一些方向。我有一些关于它没有太多清晰的认识。所以,请...
I want some directions. I have not much clear understanding about it. So please...
下面是XML格式我的编辑文本:
Here is my edit-text in xml form:
<EditText
android:id="@+id/editTextName"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_margin="3dp"
android:hint="Insert Name"
android:onClick="surNameEditTextClick" />
code,以获取输入字符串编辑文本的:
Code to get the input-string of the edit-text:
EditText nameText = (EditText) findViewById(R.id.editTextName);
String name = nameText.getText().toString();
保存名称字符串到字符串数组列表:
Saving the name string into an array-list of string:
ArrayList<String> nameArrayList = new ArrayList<String> ; //created globally
if(!(nameArrayList.contains(name))){
//Adding input string into the name array-list
nameArrayList.add(name) ;
}
把这个数组列表进入shared- preferences:
Putting this array-list into the shared-preferences:
SharedPreferences saveGlobalVariables = getSharedPreferences(APP_NAME, 0);
SharedPreferences.Editor editor = saveGlobalVariables.edit();
editor.putStringSet("name", new HashSet<String>(surNameArrayList));
editor.commit();
让所有Shared- preferences数据传回数组列表时,程序 负载(中的onCreate()):
Getting all Shared-Preferences data back to array-list when program loads (in onCreate() ):
SharedPreferences loadGlobalVariables = getSharedPreferences(APP_NAME, 0);
nameArrayList = new ArrayList<String>(loadGlobalVariables.getStringSet("name", new HashSet<String>()));
现在如何得到这个数据,根据该修改文本的一些看法形式。我已经看到了不同的方法,但不理解清楚。如果我用的是
Now how to get this data in some view form under that edit-text. I have seen different methods but not understanding clearly. If I use the
EditText mNameEditText;
mNameEditText = (EditText)findViewById(R.id.editTextName);
mNameEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s){
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count){
}
});
那么什么code snipet将在这里使用?其中的TextView或列表视图应该用在哪里??? 我无法理解。 如果任何其他方法可用,那么请在这里提供。 问候,
Then what code snipet will be used here? Which textView or list-view should be used and where??? I am unable to understand it. If any other method is available, then please provide here. regards,
您是大部分的方式出现。我建议使用 AutoCompleteTextView 并结合简单的列表到ArrayAdapter。 (一AutoCompleteTextView已经有下拉功能,以帮助类似的条目之间的用户选择,并且不需要一个TextWatcher。)
You are most of the way there. I recommend using an AutoCompleteTextView and simply binding your List to an ArrayAdapter. (An AutoCompleteTextView already has the dropdown feature to help the user select between similar entries and doesn't require a TextWatcher.)
从文件code:
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
(您可以使用ArrayList中以相同的方式对原始阵列上面。)
(You can use your ArrayList in an identical manner to the primitive Array above.)