package com.example.root.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button btnCommercial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list11 = (ListView) findViewById(R.id.listView1);
ArrayList<String> authorities = getAuthorities("AuthorityList.json");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, authorities);
btnCommercial = (Button) findViewById(R.id.btnCommercial);
btnCommercial.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
list11.setAdapter(adapter);
}
});
}
private ArrayList<String> getAuthorities(String fileName) {
JSONArray jsonArray = null;
ArrayList<String> AuthList = new ArrayList<String>();
try {
InputStream inputStream = getAssets().open(fileName); //open the inputStream to the file
int size = inputStream.available(); //size of the entire json object
byte[] data = new byte[size]; //array that will store all the data
inputStream.read(data); //reading data into the array for the file
inputStream.close(); //close the input steam
String json = new String(data, "UTF-8");
jsonArray = new JSONArray(json);
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
AuthList.add(jsonArray.getJSONObject(i).getString("description"));
}
}
} catch (IOException e) {
e.printStackTrace(); return null;
} catch (JSONException je) {
je.printStackTrace(); return null;
}
return AuthList;
}
}
からのドロップダウンメニューを作成します。 soapuiを使ってリモートサーバーにアクセスしようとしていたので、.jsonファイルを資産の下のプロジェクトにコピーしました。コードは私にエラーを与えませんが、私のアプリは実行されません。問題は、.jsonファイルを開く方法、またはオブジェクトにアクセスする方法です。.jsonファイル私は私のプロジェクトであるJSONファイルからデータをドロップダウンメニューを作成したい
ありがとうございます。
あなたは何を意味するか、アプリが実行されませんか?例外が発生してクラッシュするのですか、またはあなたのリストにデータが入っていませんか?クラッシュした場合は、LogCatを投稿してください。 – Rohan
申し訳ありませんが私はそれが私がそれをしたい、それはドロップダウンメニューを作成し、その内容を。 – che
通常の文字列配列を作成すると、ボタンを押すとドロップダウンメニューが作成されます。 – che