如何动态创建图像的网格视图
Posted
技术标签:
【中文标题】如何动态创建图像的网格视图【英文标题】:how to create a gridview of images dynamically 【发布时间】:2014-04-14 09:59:03 【问题描述】:我正在开发一个应用程序,我必须在其中动态填充图像的网格视图。我正在从服务器获取一组图像 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]);
【问题讨论】:
也许加载器使用?这样您就可以通过编程方式添加图像) @xoxol_89 你能解释一下吗.. 【参考方案1】:我推荐使用加载器。请参阅此 [文档] (http://developer.android.com/guide/components/loaders.html) 因此,您可以在 Loade 中的非 ui 线程中传输图像加载
【讨论】:
以上是关于如何动态创建图像的网格视图的主要内容,如果未能解决你的问题,请参考以下文章