2014-04-14 95 views
0

我正在一個應用程序中,我必須動態地填充圖像的網格視圖。我從服務器獲取圖像ID數組,我正在解碼JSON數組並獲取圖像ID。現在我已將所有圖像存儲在我的可繪製文件夾中,我想要顯示我從json獲取的id的圖像,但是我被卡在這一點上,我不知道這是怎麼回事。幫助 這是我的主要活動如何動態創建圖像的網格視圖

public class MainActivity extends Activity { 
GridView grid ; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
grid = (GridView)findViewById(R.id.grid_view); 

    grid.setAdapter(new Adapter(this)); 
    Button play = (Button)findViewById(R.id.play); 
    play.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      try { 
       playgame(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 




protected void playgame() throws JSONException { 
    if(cardcount >=1){ 
    BufferedReader reader=null; 
    data_to_send = "userId=" + userId ; 
    try 
    { 
    Log.e("inside try block", "get text"); 
     // Defined URL where to send data 
     URL url = new URL("http://172.16.10.5/Ankur/andapp/request_Play.php"); 
    // Send POST data request 
    URLConnection conn = url.openConnection(); 
    conn.setDoOutput(true);     
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
    wr.write(data_to_send); 
    wr.flush(); 

    // Get the server response 

    reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 

    // Read Server Response 
    while((line = reader.readLine()) != null) 
     { 
      // Append server response in string 
      sb.append(line + "\n"); 
      Log.e("inside", "while loop"); 
     } 


    play_response = sb.toString(); 
    } 
    catch(Exception ex) 
    { 

    } 
    finally 
    { 
     try 
     { 

      reader.close(); 
     } 

     catch(Exception ex) {} 
    } 
    Log.e("play response from the server", ""+play_response); 
    }else 
    { 
     Toast.makeText(getApplicationContext(), "Sorry you don't have cards.buy a new card now", Toast.LENGTH_LONG).show(); 
    } 
JSONObject jo = new JSONObject(play_response); 
pos1 = jo.getString("0"); 
pos2 = jo.getString("1"); 
pos3 = jo.getString("2"); 
pos4= jo.getString("3"); 
pos5 = jo.getString("4"); 
pos6= jo.getString("5"); 
pos7= jo.getString("6"); 
pos8= jo.getString("7"); 
pos9= jo.getString("8"); 



Log.e("value of 1st place of array", "array value "+pics[7]); 




} 
+0

也許裝載機使用?所以你可以編程添加圖片) –

+0

@ xoxol_89你能解釋一下嗎? – user3472186

回答