android中具有共享首选项的AutoCompleteTextView

android中具有共享首选项的AutoCompleteTextView,android,sharedpreferences,autocompletetextview,Android,Sharedpreferences,Autocompletetextview,在我的应用程序中,我需要显示正在登录的用户列表。在我的用户名文本字段中。 我试图存储在SharedReferences中并检索,但我做不到。我怎样才能完成它 我将数组解析为json,并将其存储在数组中,然后在arrayadapter中使用 这是我的密码: public class MainActivity extends Activity { SharedPreferences sharedpreferences; public static final String MyPREFERENCE

在我的应用程序中,我需要显示正在登录的用户列表。在我的用户名文本字段中。
我试图存储在SharedReferences中并检索,但我做不到。我怎样才能完成它

我将数组解析为json,并将其存储在数组中,然后在arrayadapter中使用

这是我的密码:

public class MainActivity extends Activity {

SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;

public static String user_id;
EditText etxt_user;
String PLAYLISTS;
String[] listt;

ArrayList<String> array;
JSONArray jArray;

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

    etxt_user = (EditText) findViewById(R.id.textView1);
    user_id = etxt_user.getText().toString();

      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        final Editor editor = sharedpreferences.edit();

        array = new ArrayList<String>();
        jArray = new JSONArray(array);

//          final JSONArray jArray = new JSONArray(array);
//            editor.putString("buttonArray", jArray.toString());
//          editor.commit();


        final ArrayList<String> array1 = new ArrayList<String>();

//  final String name = sharedpreferences.getString("buttonArray", null);

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, array);
        final AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.textView1);
        textView.setAdapter(adapter);


        Button b1= (Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                array.add(etxt_user.getText().toString());

                editor.putString("buttonArray", jArray.toString());
                editor.commit();


                String name = sharedpreferences.getString("buttonArray", null);

                System.out.println("222222222222"+jArray.toString());
                System.out.println("333333333333"+name);
                System.out.println("444444444444"+array);

                editor.putString(PLAYLISTS,etxt_user.getText().toString());

            }
        });
    }
公共类MainActivity扩展活动{
SharedReferences SharedReferences;
公共静态最终字符串MyPREFERENCES=“MyPrefs”;
公共静态字符串用户标识;
EditText etxt_用户;
字符串播放列表;
字符串[]列表;
数组列表数组;
杰索纳雷·贾雷;
@SuppressLint(“CutPasteId”)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etxt_user=(EditText)findViewById(R.id.textView1);
user_id=etxt_user.getText().toString();
SharedReferences=GetSharedReferences(MyPREFERENCES,Context.MODE\u PRIVATE);
final Editor=SharedReferences.edit();
数组=新的ArrayList();
jArray=新的JSONArray(数组);
//最终JSONArray jArray=新JSONArray(数组);
//putString(“buttonArray”,jArray.toString());
//commit();
最终ArrayList array1=新ArrayList();
//最终字符串名称=SharedReferences.getString(“buttonArray”,null);
最终ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u下拉列表\u item\u 1line,数组);
最终的AutoCompleteTextView文本视图=(AutoCompleteTextView)findViewById(R.id.textView1);
setAdapter(适配器);
按钮b1=(按钮)findViewById(R.id.button1);
b1.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
add(etxt_user.getText().toString());
putString(“buttonArray”,jArray.toString());
commit();
String name=SharedReferences.getString(“buttonArray”,null);
System.out.println(“2222”+jArray.toString());
System.out.println(“333”+名称);
System.out.println(“4444”+数组);
putString(播放列表,etxt_user.getText().toString());
}
});
}

如果数据是静态的,请将其保存在Strings.xml中,否则您可以将其保存在DB中并访问它。
这是一个可以帮助您的链接

否,它不是静态的,也没有数据库。我正在尝试使用sharedpref。首先,在SharefPreferences中保存所有用户的日志是一个坏主意,因为遍历并在edittext中显示所有用户名将非常困难。因为SharedPreferences是键值对,名称是键,密码是其值一般来说。但是你可以使用一些黑客代码。好吧,如果你知道如何迭代SharefPreferences并获取所有用户名,那么在gmail中查看这篇文章,当你开始键入你的mailid时,它会显示带有你的邮件id的自动填充选项。所以你不需要键入你的完整id,我也在尝试同样的事情。那么这很简单。String name=SharedReferences.getString(“userId”);private static final String[]COUNTRIES=new String[]{name};//在onCreate方法AutoCompleteTextView textView=(AutoCompleteTextView)findViewById(R.id.actv_country);ArrayAdapter=new ArrayAdapter(这是android.R.layout.simple_dropdown_项目_1;行,COUNTRIES);textView.setAdapter(适配器);AutoCompleteTextView的使用方式与版面中的EditText完全相同(因为它是EditText的子类)。您只需将EditText替换为AutoCompleteTextView。我试图将其存储在SharedReferences中并检索,但无法。我如何才能完成?这意味着您没有获得字符串值?是的,当用户输入并单击“登录”按钮时,我需要在SP中动态放置字符串。然后,下次当他键入其id或名称时,我需要显示自动填充选项