我正在尝试将元素(名称)从JSONArray复制到ArrayList,然后从ArrayList复制到Spinner。我没有收到任何错误消息但是在执行时Spinner没有显示任何元素。 Spinner如何显示元素?
package com.example.klarity;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.example.klarity1.R;
public class MainActivity extends ActionBarActivity {
Spinner practiceSpin;
Spinner projectSpin;
JSONObject projDet;
JSONArray projArr;
ArrayList<String> arrData=new ArrayList<String>();
int[] project;
public final static String JSON_DATA =
"{"
+ " \"project\": ["
+ " {"
+ " \"name\": \"xxxx\""
+ " },"
+ " {"
+ " \"name\": \"yyyy\""
+ " }," //JSONArray created
+ " {"
+ " \"name\": \"zzzz\""
+ " },"
+ " {"
+ " \"name\": \"hhhh\""
+ " }"
+ " ]"
+ "}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
projDet=new JSONObject(JSON_DATA);
projArr=projDet.getJSONArray("project");
ArrayList<String> arrData=new ArrayList<String>();
for(int n = 0; n < projArr.length(); n++)
{
//JSONObject projDet = projArr.getJSONObject(n);
arrData.add(projArr.getJSONObject(n).getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("exception occured");
}
try {
practiceSpin=(Spinner) findViewById(R.id.spinnerPractice);
ArrayAdapter<CharSequence> ar= ArrayAdapter.createFromResource(this,R.array.practices ,android.R.layout.simple_list_item_1);
ar.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
practiceSpin.setAdapter(ar);
/*projectSpin=(Spinner) findViewById(R.id.spinnerProject);
ArrayAdapter<CharSequence> ar1= ArrayAdapter.createFromResource(this,R.array.projects ,android.R.layout.simple_list_item_1);
ar1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
projectSpin.setAdapter(ar1);*/
ArrayAdapter<String> adapter_proj = new ArrayAdapter<String>(this,R.array.projects, android.R.layout.simple_spinner_item,arrData); //"The constructor ArrayAdapter<String>(MainActivity, int, int, JSONArray)
adapter_proj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
projectSpin.setAdapter(adapter_proj);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println(e1.getMessage());
System.out.println("exception occured in 2nd catch");
}
finally{
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
问题在于您的本地varibable名为arrData
:替换
ArrayList<String> arrData=new ArrayList<String>();
用这个
this.arrData=new ArrayList<String>();
(删除ArrayList)。
arrData
仅存在于第一个try-cacth块中,而在第二个try-catch块中,arrData
恰好是您班级的字段。
我强烈建议您尽可能使用“this”以避免类似的问题。