【问题标题】:Android: Save Selected Items from spinner to SharedPreferencesAndroid:将选定项目从微调器保存到 SharedPreferences
【发布时间】:2019-05-09 01:20:29
【问题描述】:

从我上面的问题,我实际上想测试我从互联网上获得的现有代码。我成功地可以从表中的列中获取数据并显示数据。例如,当我选择人名的“Lim AI Khoon”时,它会在同一活动中显示 Lim Ai Khoon 姓名,以及 Lim Ai Khoon 的徽章 ID。现在,如何将已选择的数据保存到 sharedPreferences 并在下一个活动中显示数据?下面是我的代码 //MainActivity.java

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

    //Initializing the ArrayList
    students = new ArrayList<String>();

    //Initializing Spinner
    spinner = (Spinner) findViewById(R.id.spinner);

    //Adding an Item Selected Listener to our Spinner
    //As we have implemented the class Spinner.OnItemSelectedListener to this class iteself we are passing this to setOnItemSelectedListener
    spinner.setOnItemSelectedListener(this);

    //Initializing TextViews
    tvName = (TextView) findViewById(R.id.tvName);
    tvBadgeID = (TextView) findViewById(R.id.tvBadgeID);
    btnNext = findViewById(R.id.btnNext);


    //This method will fetch the data from the URL
    getData();

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Main2Activity.class);
            startActivity(intent);
        }
    });
}

private void getData(){
    //Creating a string request
    StringRequest stringRequest = new StringRequest(Config.DATA_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    JSONObject j = null;
                    try {
                        //Parsing the fetched Json String to JSON Object
                        j = new JSONObject(response);

                        //Storing the Array of JSON String to our JSON Array
                        result = j.getJSONArray(Config.JSON_ARRAY);

                        //Calling method getStudents to get the students from the JSON Array
                        getStudents(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    //Creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //Adding request to the queue
    requestQueue.add(stringRequest);
}

private void getStudents(JSONArray j){
    //Traversing through all the items in the json array
    for(int i=0;i<j.length();i++){
        try {
            //Getting json object
            JSONObject json = j.getJSONObject(i);

            //Adding the name of the student to array list
            students.add(json.getString(Config.TAG_NAME));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    //Setting adapter to show the items in the spinner
    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, students));

    SharedPreferences sharedPref = getSharedPreferences("MyData", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("reviewer",spinner.getSelectedItem().toString());
}


//Method to get student name of a particular position
private String getName(int position){
    String name="";
    try {
        //Getting object of given index
        JSONObject json = result.getJSONObject(position);

        //Fetching name from that object
        name = json.getString(Config.TAG_NAME);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    //Returning the name
    return name;
}

//Doing the same with this method as we did with getName()
private String getCourse(int position){
    String course="";
    try {
        JSONObject json = result.getJSONObject(position);
        course = json.getString(Config.TAG_BADGEID);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return course;
}


//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    //Setting the values to textviews for a selected item
    tvName.setText(getName(position));
    tvBadgeID.setText(getCourse(position));
}

//When no item is selected this method would execute
@Override
public void onNothingSelected(AdapterView<?> parent) {
    tvName.setText("");
    tvBadgeID.setText("");
}

//Config.java

public class Config {
//JSON URL
public static final String DATA_URL = "http://10.0.2.2/spinner/getData.php";

//Tags used in the JSON String
public static final String TAG_NAME = "name";
public static final String TAG_BADGEID = "badgeid";

//JSON array name
public static final String JSON_ARRAY = "result";}

【问题讨论】:

  • 我能知道问题出在哪里 - 我们能知道问题出在哪里吗?有错误吗?
  • 错误,微调器无法从学生表的“名称”列加载数据
  • er,这个错误发生在哪里? PHP 也许?
  • 是的,php。 PHP 出错
  • students 表中可能没有名为 name 的列

标签: java php android mysql


【解决方案1】:

在这之后你错过了一步

editor.putString("reviewer",spinner.getSelectedItem().toString());

你需要保存更改,将这一行放在下面:

editor.commit();

应该可以。

【讨论】:

  • 当我点击下一步按钮时。它只在微调器处显示第一个列表,尽管我在微调器处单击第二个列表
猜你喜欢
  • 2019-05-24
  • 1970-01-01
  • 2011-07-01
  • 2019-01-05
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多