###前提・実現したいこと
初めての質問です。Java、android開発歴は2か月程の初級者ですがよろしくお願いします。
囲碁の問題を出題するアプリを作っているのですが、問題リストをリストビューで表示する段階で躓いています。
jsonファイルから問題情報を読み込むところまではできてますが、ListViewにそのデータを表示する段階のどこかでおかしいことをしているようです。
デバッグ、Log出力も試してみましたが原因は掴めず。
突っ込みどころの多いソースコードだとは思いますが、よければ改善策をご教授頂けないでしょうか?
よろしくお願いします。
###発生している問題・エラーメッセージ
エラーは起きていませんが、リストビューに何も表示されません。
###該当のソースコード
GoDataAdapter.java
Java
1 2------------省略---------------- 3 4 public GoDataAdapter(Context context){ 5 super(context, 0); 6 mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 7 } 8 9 public void setGoDataArrayList(ArrayList<GoData> goDataArrayList){ 10 mGoDataArrayList = goDataArrayList; 11 } 12 13 @NonNull 14 @Override 15 public View getView(int position, View convertView, @NonNull ViewGroup parent) { 16 17 if (convertView == null){ 18 convertView = mLayoutInflater.inflate(R.layout.list_item, null); 19 } 20 21 TextView q_num_level =(TextView) convertView.findViewById(R.id.q_num_level); 22 TextView teban = (TextView) convertView.findViewById(R.id.teban); 23 24 q_num_level.setText(mGoDataArrayList.get(position).getQNum() + " " + 25 mGoDataArrayList.get(position).getLevel()); 26 teban.setText(mGoDataArrayList.get(position).getTeban()); 27 28 return convertView; 29 } 30}
###追記
ProblemList.java
Java
1 2------------省略---------------- 3 4 @Override 5 protected void onCreate(@Nullable Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.problem_list); 8 9 //Realmの設定 10 mRealm = Realm.getDefaultInstance(); 11 mRealm.addChangeListener(mRealmListener); 12 mGoDataRealmResults = mRealm.where(GoData.class).findAll(); //ここでdata : size = 1になった 13 14 //ListViewの設定 15 mListView = (ListView) findViewById(R.id.listView1); 16 17 //ListViewをタップしたときの処理 18 mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 19 @Override 20 public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { 21 22 GoData goData = (GoData) parent.getAdapter().getItem(pos); 23 24 Intent intent = new Intent(ProblemList.this, Problem.class); 25 intent.putExtra(EXTRA_GODATA, goData); 26 27 startActivity(intent); 28 } 29 }); 30 } 31 32 @Override 33 public void onResume() { 34 super.onResume(); 35 36 if (mGoDataAdapter == null) { 37 List<GoData> dataList = null; 38 try { 39 dataList = loadGoData(); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } 43 44 mGoDataAdapter = new GoDataAdapter(this); 45 mGoDataAdapter.setGoData(dataList); 46 47 //ListViewに表示 48 mListView.setAdapter(mGoDataAdapter); 49 mGoDataAdapter.notifyDataSetChanged(); 50 } 51 addGoData(); 52 53 reloadListView(); 54 } 55 56 @Override 57 protected void onDestroy() { 58 super.onDestroy(); 59 60 mRealm.close(); 61 } 62 63 public List<GoData> loadGoData() throws IOException { 64 65 loadJsonFromStream(); 66 67 return mRealm.where(GoData.class).findAll(); 68 } 69 70 private void loadJsonFromStream() throws IOException { 71 72 BufferedReader br; 73 String json = ""; 74 75 InputStream is = getAssets().open("sgfdata.json"); 76 77 mRealm.beginTransaction(); 78 try { 79 br = new BufferedReader((new InputStreamReader(is))); 80 String s; 81 while ((s = br.readLine()) != null) { 82 json += s; 83 } 84 85 JSONObject jsonObject = new JSONObject(json); 86 ZahyoChanger changer = new ZahyoChanger(); 87 JSONArray normal_problem = jsonObject.getJSONObject("problems").getJSONArray("normal"); 88 for (int i = 0; i < normal_problem.length(); i++) { 89 String value1 = normal_problem.getString(i); 90 changer.GoDataSeparate(value1); 91 } 92 JSONArray normal_answers = jsonObject.getJSONObject("answers").getJSONArray("normal"); 93 for (int j = 0; j < normal_answers.length(); j++) { 94 String value2 = normal_answers.getString(j); 95 changer.GoDataSeparate(value2); 96 mRealm.createAllFromJson(GoData.class, is); 97 mRealm.commitTransaction(); 98 } 99 } catch (IOException | JSONException e) { 100 mRealm.cancelTransaction(); 101 } finally { 102 if (is != null) { 103 is.close(); 104 } 105 } 106 } 107 108 private void reloadListView() { 109 110 ArrayList<GoData> goDataArrayList = new ArrayList<>(); 111 112 int[] icons = { 113 R.mipmap.ic_launcher, 114 R.mipmap.ic_launcher 115 }; 116 117 for (int i = 0; i < mGoDataRealmResults.size(); i++) { 118 119 GoData data = new GoData(); 120 121 data.setQNum(mGoDataRealmResults.get(i).getQNum()); 122 123 goDataArrayList.add(data); 124 } 125 126 mGoDataAdapter = new GoDataAdapter(this); 127 mGoDataAdapter.setGoData(goDataArrayList); 128 mListView.setAdapter(mGoDataAdapter); 129 mGoDataAdapter.notifyDataSetChanged(); 130 } 131 132 private void addGoData() { 133 134 if(mGoDataRealmResults.size() > 0){ 135 136 mRealm.beginTransaction(); 137 138 GoData data = new GoData(); 139 data.setQNum("1"); 140 data.setTeban("b"); 141 data.setLevel("-5"); 142 143 mRealm.commitTransaction(); 144 } 145 } 146}
###試したこと
Java
1if (convertView == null){ 2 convertView = mLayoutInflater.inflate(R.layout.list_item, null);
GoDataAdapterクラス内の上記のコードの(R.layout.list_item, null)は、(R.layout.simple_list_item_2, null)だったところを自作のxmlファイルに変更していますが、リストビューは表示されません。
別のところに原因があるのだと思っています。
###補足情報(言語/FW/ツール等のバージョンなど)
言語:Java
開発ツール:androidstudio 2.2.1
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/17 06:11
2016/10/17 06:22
退会済みユーザー
2016/10/17 07:40
2016/10/17 08:16
2016/10/17 10:19
2016/10/17 10:25
2016/10/17 11:00
2016/10/17 11:21
2016/10/17 11:57
退会済みユーザー
2016/10/18 01:41
2016/10/21 06:16