Android: JSON data loaded into adapter

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.

+4
source share
3 answers

Hope this helps ...

 try{ jArray = new JSONArray(result); JSONObject json_data=null; Games game_data[]; game_data[] = new Games[jArray.length()]; 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"); game_data[i] = 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); 

}

+1
source

I think Games is your POJO class, so you can do something like this,

Create a POJO Class List

 private List<Games> games= new ArrayList<Games>(); 

Now you can add your content to the list of 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.add(new Games(ct_id, ct_name)); } 

Finally, pass the list in the adapter.

 GameAdapter adapter = new GameAdapter(this, R.layout.listview_item_row, games); 
+1
source

error: the variable must either provide dimension expressions or an array initializer.

=> This error is obvious because you did not define the dimension values ​​for creating the array, and also did not pass any initialization to this array.

So, I think you are doing it wrong, instead, do it like this:

 Games game_data[] = new Games(ct_id, ct_name); 

This is the correct way to call the constructor.

And I don’t know what you wrote inside the Games class, so I just mentioned the constructor call.

0
source

Source: https://habr.com/ru/post/1383366/


All Articles