It may be a simple Java problem that I am struggling with, but I am looking at the community to help me here, as I came across a brick wall with this situation.
I have data coming from a MySQL database and delivered to the application via JSON. During the parsing, I tried to create another array that should be passed to the ArrayAdapter, which will be used in the ListView. Here is the code I'm having a problem with:
try{ jArray = new JSONArray(result); JSONObject json_data=null; for(int i=0;i<jArray.length();i++){ json_data = jArray.getJSONObject(i); ct_id=json_data.getString("ID"); ct_name=json_data.getString("Player2N"); Games game_data[] = new Games[] { new Games(ct_id, ct_name) }; } GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, game_data); listView1 = (ListView)findViewById(R.id.listView1); View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null); listView1.addHeaderView(header); listView1.setAdapter(adapter); }
This line: GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, game_data);
In particular, game_data is highlighted in red in Eclipse. Where am I curious why game_data gets out of hand after the loop ends? I'm just trying to add specific fields in a line from JSON to the adapter here.
I also tried to go through the loop in array setup, but still don't play dice, as new games [] get an error. Here is an example:
Games game_data[] = new Games[] { for(int i=0;i<jArray.length();i++){ json_data = jArray.getJSONObject(i); ct_id=json_data.getString("ID"); ct_name=json_data.getString("Player2N"); // Games game_data[] = new Games[] // { new Games(ct_id, ct_name); // }; }
error: the variable must either provide dimension expressions or an array initializer.
source share