在微调器中选择项目后,在textview中显示相应的json数据

时间:2017-02-16 05:52:46

标签: android json spinner

我的微调器包含来自json的唯一标准值(比如:我的微调器只包含7,8,6(我想要的),而不是显示所有标准的复制数据),如果选择了微调器项,则它将获取相应的有关以该选定标准学习的学生的所有信息。这是我的代码,

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

ArrayList<String> AllStandards = new ArrayList<>();
ArrayAdapter<String> adapter;
JSONArray jsonArray;
Spinner spinner;

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

    final List<String> items = getCountries("data.json");

    spinner = (Spinner) findViewById(R.id.spinnerStandard);
    adapter = new ArrayAdapter<String>(this, R.layout.second_layout, R.id.txtStandard, items);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            try {
                Intent intent = new Intent(getApplicationContext(), StudentsInfo.class);
                intent.putExtra("name", jsonArray.optJSONObject(i).getString("name"));
                intent.putExtra("surname", jsonArray.optJSONObject(i).getString("surname"));
                intent.putExtra("age", jsonArray.optJSONObject(i).getString("age"));
                intent.putExtra("div", jsonArray.optJSONObject(i).getString("div"));

                startActivity(intent);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    spinner.setAdapter(adapter);

}//onCreate Method

private List<String> getCountries(String fileName) {
    jsonArray = null;


    //ArrayList<String> cList = new ArrayList<String>();
    try {
        InputStream is = getResources().getAssets().open(fileName);
        int size = is.available();
        byte[] data = new byte[size];
        is.read(data);
        is.close();
        String json = new String(data, "UTF-8");

        AllStandards.clear();
        try {
            jsonArray = new JSONArray(json);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String stand = jsonObject.getString("standard");
                if (!AllStandards.contains(stand)) {
                    AllStandards.add(stand);
                }
            }
        }
        catch (JSONException je) {
            je.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }catch (IOException e) {
        e.printStackTrace();
    }
    return AllStandards;
}
}

这里是json数据,

[
{
"name":"aaa",
"surname":"bbb",
"age":"18",
"div":"A",
"standard":"7"
},
{
"name":"ccc",
"surname":"ddd",
"age":"17",
"div":"B",
"standard":"7"
},
{
"name":"eee",
"surname":"fff",
"age":"18",
"div":"A",
"standard":"8"
},
{
"name":"ggg",
"surname":"hhh",
"age":"17",
"div":"A",
"standard":"7"
},
{
"name":"sss",
"surname":"ddd",
"age":"18",
"div":"A",
"standard":"8"
},
{
"name":"www",
"surname":"ggg",
"age":"17",
"div":"A",
"standard":"7"
},
{
"name":"ggg",
"surname":"ccc",
"age":"18",
"div":"B",
"standard":"6"
}

但问题是,当我从我的微调器中选择标准7时,它只显示一个学生信息。但是我希望所有学生都能获得7个标准的学习信息。这应该发生在旋转器中的所有选择器(就像我的意思是如果我选择标准8然后它应该显示所有学生信息谁都在学习8标准,相同的标准6)

如果我从微调器中选择标准7,那么它应该显示学生aaa,学生ccc,学生ggg,学生www的所有信息,因为他们都在标准7中学习。

我尝试使用谷歌搜索找到这个问题的解决方案,但我没有找到任何适合我的问题的答案。我已经检查了stackoverflow的两个帖子,但他们还没有答案。

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

因此,您希望从所选标准中获取所有学生的详细信息。以下是解决问题的代码。

使用getter和setter的

创建值boject类
public class StudentVO implements Serializable{

    private String name;
    private String surname;
    private String age;
    private String div;
    private String standard;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDiv() {
        return div;
    }

    public void setDiv(String div) {
        this.div = div;
    }

    public String getStandard() {
        return standard;
    }

    public void setStandard(String standard) {
        this.standard = standard;
    }
}

现在,在您的主要类中,您从spinner中选择标准代替您的代码:

//Add this
public class MainActivity extends AppCompatActivity {

ArrayList<String> AllStandards = new ArrayList<>();
private ArrayList<StudentVO> studentVOList = new ArrayList<>();
ArrayAdapter<String> adapter;
JSONArray jsonArray;
Spinner spinner;

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

    final List<String> items = getCountries("data.json");

    spinner = (Spinner) findViewById(R.id.spinnerStandard);
    adapter = new ArrayAdapter<String>(this, R.layout.second_layout, R.id.txtStandard, items);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            try {

                String standard = AllStandards.get(i);

                if (studentVOList.size() > 0)
                    studentVOList.clear();
                for (int j = 0; j < jsonArray.length(); j++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(j);
                    String stand = jsonObject.getString("standard");
                    if (stand.equalsIgnoreCase(standard)) {
                        StudentVO studentVO = new StudentVO();
                        studentVO.setAge(jsonObject.getString("age"));
                        studentVO.setName(jsonObject.getString("name"));
                        studentVO.setDiv(jsonObject.getString("div"));
                        studentVO.setStandard(stand);
                        studentVOList.add(studentVO);
                    }
                }

                Log.d("TAG", "List With All Students in selected standard: "+studentVOList.size());

                Intent intent = new Intent(getApplicationContext(), StudentsInfo.class);
                Bundle b = new Bundle();
                b.putSerializable("list",studentVOList);

                intent.putExtra("bundle",b);

                startActivity(intent);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });
    spinner.setAdapter(adapter);

}//onCreate Method

private List<String> getCountries(String fileName) {
    jsonArray = null;


    //ArrayList<String> cList = new ArrayList<String>();
    try {
        InputStream is = getResources().getAssets().open(fileName);
        int size = is.available();
        byte[] data = new byte[size];
        is.read(data);
        is.close();
        String json = new String(data, "UTF-8");

        AllStandards.clear();
        try {
            jsonArray = new JSONArray(json);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String stand = jsonObject.getString("standard");
                if (!AllStandards.contains(stand)) {
                    AllStandards.add(stand);
                }
            }
        }
        catch (JSONException je) {
            je.printStackTrace();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }catch (IOException e) {
        e.printStackTrace();
    }
    return AllStandards;
}
}

在此之后,您可以在StudentsInfo类中获取此列表,如:

Bundle b = getIntent().getBundleExtra("bundle");
ArrayList<StudentVO> studentVOList = (ArrayList<StudentVO>) b.getSerializable("list");

Log.d("TAG", "List Size; "+studentVOList.size());

而且您不需要从json数组中获取标准列表。您可以使用标准1,2,3 ....准备静态学生列表,然后将其传递给您的阵列适配器。

查看我的调试结果: enter image description here

我只是查看您的代码 删除

intent.putExtra("name", jsonArray.optJSONObject(i).getString("name"));
                    intent.putExtra("surname", jsonArray.optJSONObject(i).getString("surname"));
                    intent.putExtra("age", jsonArray.optJSONObject(i).getString("age"));
                    intent.putExtra("div", jsonArray.optJSONObject(i).getString("div"));
来自您的MainActivity和StudentInfo活动类的onCreate()方法的代码您可以获得以下学生列表:

Bundle b = getIntent().getBundleExtra("bundle");
    ArrayList<StudentVO> studentVOList = (ArrayList<StudentVO>) b.getSerializable("list");

答案 1 :(得分:0)

因为在你的微调器itemSelected监听器中你得到jsonArray.optJSONObject(i)这里我是微调项目的位置,而不是标准。 您将从spinner

获得此标准的值
spinner.getItemAtPosition(i);

并将其与JSONArray中所有元素的标准相匹配,并将其添加到列表中。 然后在你的意图中传递这个列表。

intent.putStringArrayListExtra("test", (ArrayList<String>) test);