首先,当我运行程序时,它工作正常,列表元素被发送到微调器....但下次我得到空指针异常 spinner.setAdapter(spinnerArrayAdapter); 请帮帮我..
InputStream in = response.getEntity().getContent(); //Get the data in the entity
String a=convertStreamToString(in);
JSONObject jObject = new JSONObject(a);
int result = jObject.getInt("status");
final JSONObject banksObj = jObject.getJSONObject("message");
ArrayList<String> bankNumbersList =new ArrayList<String>();
ArrayList<String> bankNamesList =new ArrayList<String>();
Iterator<String> keys = banksObj.keys(); //json is JsonObject From which you get all information
while (keys.hasNext())
{
String key = keys.next();
Log.d("key",key);
bankNumbersList.add(key);
try
{
String bankName = banksObj.getString(key);
Log.d("bank",bankName);
bankNamesList.add(bankName);
}
catch (JSONException e)
{
// Handle Exception
}
}
String[] numbers = new String[bankNumbersList.size()];
numbers = bankNumbersList.toArray(numbers);
Log.d("numbers",bankNumbersList.toString());
String[] names = new String[bankNamesList.size()];
names = bankNamesList.toArray(names);
Log.d("names",bankNamesList.toString());
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
Log.d("message","helll");
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(Registration.this,android.R.layout.simple_spinner_item, bankNamesList);
//You can create Spinner for Numbers just change numbers
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if (!bankNamesList.equals(""))
spinner.setAdapter(spinnerArrayAdapter);
if(result==1)
{
Log.d("message","hellllllllllo");
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Log.d("banks",banks);
Toast.makeText( Registration.this, "Success", Toast.LENGTH_LONG).show();
}
});
}else if(result==0){
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText( Registration.this, "Unsuccessful",Toast.LENGTH_LONG).show();
}
});
}
}
}
catch(final Exception e){
Log.d("exception",e.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText( Registration.this,
"Unable to connect. Please try again.++++++++++ ",
Toast.LENGTH_LONG).show();
}
});
xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccdddd" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:background="#00716f" >
<TextView
android:id="@+id/registration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Registration"
android:textColor="#fff"
android:textSize="18sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:src="@drawable/backicon" />
</RelativeLayout>
<ScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="700dp"
android:layout_below="@id/relativeLayout1">
<Spinner
android:id="@+id/spinner1"
android:layout_width="300dp"
android:layout_height="45dp"
android:layout_marginTop="19dp"
android:layout_below="@+id/name"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:0)
要从Json获取所有键和值,您可以使用以下代码
ArrayList alKeys=new ArrayList();
ArrayList alValues=new ArrayList();
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
alKeys.add(key);
try {
String value = json.getString(key);
alValues.add(value);
} catch (JSONException e) {
e.printStacktrace();
}
}
答案 1 :(得分:0)
使用keys()迭代器迭代所有属性,并为每个属性调用get()。
JSONObject jObject = new JSONObject(a);
int result = jObject.getInt("status");
final String banks = jObject.getString("message");
ArrayList<String> bankNumbersList =new ArrayList<String>();
ArrayList<String> bankNamesList =new ArrayList<String>();
JSONObject banksObj = jObject.getJSONObject("message");
Iterator<String> iter = banksObj.keys();
while (iter.hasNext()) {
String key = iter.next();
bankNumbersList.add(key);
try {
String bankName = banksObj.getString(key);
bankNamesList.add(bankName);
} catch (JSONException e) {
// Something went wrong!
}
}
答案 2 :(得分:0)
ArrayList<String> bankNumbers =new ArrayList<String>();
ArrayList<String> bankNames =new ArrayList<String>();
Iterator<String> keys = json.keys(); //json is JsonObject From which you get all information
while (keys.hasNext())
{
String key = keys.next();
bankNumbers.add(key);
try
{
String bankName = json.getString(key);
bankNames.add(bankName);
}
catch (JSONException e)
{
// Handle Exception
}
}
String[] numbers = new String[bankNumbers.size()];
numbers = bankNumbers.toArray(numbers);
String[] names = new String[bankNames.size()];
names = bankNames.toArray(names);
//在spinner中添加值:
Spinner spinner = (Spinner) findViewById(YOUR_SPINNER_ID);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, names); //You can create Spinner for Numbers just change numbers
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);
是的。