我将我的数据放入spinners.Data来自URL。如何制作微调器以便当教学大纲选择ICSE时它只反映那些有ICSE的成绩?
我的JSON
{
"results":[
{
"syllabus":"CBSE",
"grade":"5",
"subject":"Kannada",
"topic":"Grammar Level 1",
"id":28
},
{
"syllabus":"CBSE",
"grade":"5",
"subject":"Science",
"topic":"The Natural Calamities",
"id":16
},
{
"syllabus":"CBSE",
"grade":"6",
"subject":"Science",
"topic":"Movement in Animals",
"id":116
},
{
"syllabus":"CBSE",
"grade":"6",
"subject":"Social Studies",
"topic":"Contact With Distant Lands",
"id":59
},
{
"syllabus":"CBSE",
"grade":"6",
"subject":"Social Studies",
"topic":"Gupta Empire",
"id":34
},
"id":148
},
{
"syllabus":"ICSE",
"grade":"6",
"subject":"Computers",
"topic":"Introduction to QBASIC Programming",
"id":44
},
{
"syllabus":"ICSE",
"grade":"6",
"subject":"Social Studies - History",
"topic":"The Vedic Age",
"id":42
},
{
"syllabus":"Entrance Exam - Karnataka CET",
"grade":"12",
"subject":"Mathematics",
"topic":"Previous Year Question Papers",
"id":121
},
{
"syllabus":"Entrance Exam - Karnataka CET",
"grade":"12",
"subject":"Science - Biology",
"topic":"Previous Year Question Papers",
"id":117
},
{
"syllabus":"Entrance Exam - Karnataka CET",
"grade":"12",
"subject":"Science - Chemistry",
"topic":"Previous Year Question Papers",
"id":110
},
{
"syllabus":"Entrance Exam - Karnataka CET",
"grade":"12",
"subject":"Science - Physics",
"topic":"Previous Year Question Papers",
"id":104
}
}
任何帮助对我来说都很棒
MainActivity Class
public class MainActivity extends Activity {
JSONObject jsonobject;
JSONArray jsonarray;
JSONArray jsonarray2;
JSONObject jsonobject2;
ArrayList<String> worldlist;
ArrayList<String> worldlist2;
ArrayList<String> worldlist3;
ArrayList<Results> world;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.topic);
// Download JSON file AsyncTask
new DownloadJSON().execute();
}
// Download JSON file AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
world = new ArrayList<Results>();
// Create an array to populate the spinner
worldlist = new ArrayList<String>();
worldlist2 = new ArrayList<String>();
worldlist3 = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions
.getJSONfromURL("https://www.wonderslate.com/funlearn/topicsMap");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("results");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Results worldpop = new Results();
worldpop.setSyllabus(jsonobject.optString("syllabus"));
worldpop.setGrade(jsonobject.optString("grade"));
worldpop.setSubject(jsonobject.optString("subject"));
worldpop.setId(jsonobject.optString("id"));
world.add(worldpop);
worldlist.add(jsonobject.optString("syllabus"));
worldlist2.add(jsonobject.optString("grade"));
worldlist3.add(jsonobject.optString("subject"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the spinner in activity_main.xml
final Spinner mySpinner = (Spinner) findViewById(R.id.syllabus);
final Spinner mySpinner2 = (Spinner) findViewById(R.id.grade);
Spinner mySpinner3 = (Spinner) findViewById(R.id.subject);
// Spinner adapter
final LinkedHashSet<String>[] listToSet = new LinkedHashSet[]{new LinkedHashSet<String>(worldlist)};
//Creating Arraylist without duplicate values
final List<String> worldlistnew = new ArrayList<String>(listToSet[0]);
mySpinner
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlistnew));
//Creating ArrayList without duplicate values
listToSet[0] = new LinkedHashSet<String>(worldlist3);
//Creating Arraylist without duplicate values
final List<String> worldlistnew3 = new ArrayList<String>(listToSet[0]);
mySpinner3
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlistnew3));
// Spinner on item click listener
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
if (mySpinner.getSelectedItem().toString().equals("CBSE")) {
// if (mySpinner.getId() == R.id.syllabus)
listToSet[0] = new LinkedHashSet<String>(worldlist2);
final List<String> worldlistnew2 = new ArrayList<String>(listToSet[0]);
mySpinner2
.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
worldlistnew2));
textView.setText("21");
} else if (mySpinner.getSelectedItem().toString().equals("ICSE")) {
textView.setText("31");
} else if (mySpinner.getSelectedItem().toString().equals("Entrance Exam - Karnataka CET"))
{
textView.setText("41");
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
private void resetGrade(String grade) {
}
}
答案 0 :(得分:0)
在第一个微调器的onitemclick监听器中,根据所选项目的不同,更改第二个微调器的列表数据并调用。
spinner2Adapter.notifyDatasetChanged();
每次更改spinner1中的选择时,都不必创建新的适配器和列表。
答案 1 :(得分:0)
首先从doInBackground方法中删除这些行。这里不需要创建单独的arraylist ..
worldlist.add(jsonobject.optString("syllabus"));
worldlist2.add(jsonobject.optString("grade"));
worldlist3.add(jsonobject.optString("subject"));
全球宣布这些arraylist ......
ArrayList<String> syllabuslist;
ArrayList<String> gradlist;
ArrayList<String> subjectlist;
并在调用异步任务(DownloadJSON())之前在OnCreate方法中初始化这些微调器
Spinner mySpinner = (Spinner) findViewById(R.id.syllabus);
Spinner mySpinner2 = (Spinner) findViewById(R.id.grade);
Spinner mySpinner3 = (Spinner) findViewById(R.id.subject);
现在在onPostExecute()方法中设置微调器....删除你在onPostExcute中编写的额外代码...只需复制我的代码
@Override
protected void onPostExecute(Void args) {
// Spinner adapter
syllabuslist = new ArrayList<String>();
for (Results bean : world) {
syllabuslist.add(bean.getSyllabus());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, syllabuslist);
adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String syllabus_name = mySpinner.getItemAtPosition(arg2).toString();
gradlist = new ArrayList<String>();
for (int i = 0; i < world.size(); i++) {
if (world.get(i).getSyllabus().equals(syllabus_name)) {
gradlist.add(world.get(i).getGrade());
}
}
setGradeSpinner();//its a method to create grade spinner
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void setGradeSpinner(){
if(gradlist!=null && gradlist.size()>0)
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, gradlist);
adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner2.setAdapter(adapter);
mySpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String grade_name = mySpinner2.getItemAtPosition(arg2).toString();
subjectlist = new ArrayList<String>();
for (int i = 0; i < world.size(); i++) {
if (world.get(i).getGrade().equals(grade_name)) {
subjectlist.add(world.get(i).getSubject());
}
}
// setSubjectSpinner();//its a method to create subject spinner like as I do for garde spinner
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
希望这会对你有帮助......
答案 2 :(得分:0)
@Shwetabh Singh我无法理解你的问题。但我做了一个城市选择例子。你可以参考它。在第一个Spinner我列出了城市名称。并根据城市选择第二个微调区域被反射
{
"City": [
{
"name": "Ahmedabad",
"City_area1" : "Ghatlodia",
"City_area2" : "Chandlodia",
"City_area3" : "Other"
},
{
"name": "Hydarabad",
"City_area1" : "IIT",
"City_area2" : "Faridabad",
"City_area3" : "Other"
},
{
"name": "Noida",
"City_area1" : "Sector-50",
"City_area2" : "Sector-41",
"City_area3" : "Other"
}
]
}
MainActivity.java
public class MainActivity extends Activity {
Spinner selectcityspinner;
ArrayList<String> citylist;
ArrayList<city> cityandarealist;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectcityspinner = (Spinner)findViewById(R.id.selectcityspin);
citylist = new ArrayList<>();
new cityAsynTask().execute("Jason data url");//put your url over here
}
public class cityAsynTask extends AsyncTask<String,Void,Boolean>
{
@Override
protected Boolean doInBackground(String... params) {
cityandarealist = new ArrayList<city>();
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jobjcity = new JSONObject(data);
JSONArray jarray = jobjcity.getJSONArray("City");
for (int i = 0;i < jarray.length();i++){
city city = new city();
JSONObject jrealobject = jarray.getJSONObject(i);
city.setCity(jrealobject.getString("name"));
city.setArea1(jrealobject.getString("City_area1"));
city.setArea2(jrealobject.getString("City_area2"));
city.setArea3(jrealobject.getString("City_area3"));
cityandarealist.add(city);
citylist.add(jrealobject.getString("name"));
}
return true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if(aBoolean == false){
Toast.makeText(MainActivity.this,"data isnot recieved",Toast.LENGTH_SHORT).show();
} else {
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_item,citylist);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectcityspinner.setAdapter(adapter);
selectcityspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
String Country = String.valueOf(parent.getItemAtPosition(position));
Spinner selectareaspinner = (Spinner) findViewById(R.id.selectareaspin);
List<String> AreaList = new ArrayList<String>();
AreaList.add(cityandarealist.get(position).getArea1());
AreaList.add(cityandarealist.get(position).getArea2());
AreaList.add(cityandarealist.get(position).getArea3());
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, AreaList);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter1.notifyDataSetChanged();
selectareaspinner.setAdapter(adapter1);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
}
}