Dustin
Dustin

Reputation: 216

Android: JSON data loaded into adapter

This might be a simple Java issue I'm struggling with but I'm looking to the community to assist me here as I have hit a brick wall with this situation.

I successfully have data coming in off of a MySQL database and brought to the application through JSON. During parsing I was trying to create another array to be passed onto an ArrayAdapter to be used in a ListView. Here is the code I'm having an issue 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);

More specifically the game_data is highlighted red in Eclipse. Where I am curious is why does game_data get out of reach after end of loop? I'm just trying to add specific fields within the row from JSON to the adapter here.

I also tried going through the loop within setting up the array but still no dice as new Games[] gets 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: Variable must either provide dimension expressions or array initializer.

Upvotes: 4

Views: 1231

Answers (3)

Umesh
Umesh

Reputation: 4256

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);

}

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67296

I am thinking that Games is your POJO class so you can better do something like this,

Create a List of the POJO class

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

Now you can add your content in the games list.

              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));
              }

And finally pass the list in the Adapter.

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

Upvotes: 1

Paresh Mayani
Paresh Mayani

Reputation: 128458

error: Variable must either provide dimension expressions or array initializer.

=> This error is obvious because you haven't define any dimensions value to create an array and also haven't passed any initialization to this array.

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

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

This is the correct way to call a constructor.

And i don't know what you have written inside the Games class so i just have mentioned about the calling of constructor.

Upvotes: 0

Related Questions