有人请帮我在 Android Grid View 中放置一个图像(从 URL 的网格视图中显示图像)和 Edittextbox
这是我的网格视图 xml 布局:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<GridView
android:id="@+id/order_details_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="4"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
下面是我用来填充网格的代码,它工作正常,我需要放置来自 url 和 edittextbox 的图像:
super.onCreate(savedInstanceState);
setContentView(R.layout.order_details);
File yourFile = new File("/sdcard/json/orderdetails.txt");
FileInputStream stream = null;
try {
stream = new FileInputStream(yourFile);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String jString = "";
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder.*/
jString = Charset.defaultCharset().decode(bb).toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String fileContent=jString;
JSONObject jobj = null;
JSONObject jobj1 = null;
try {
jobj = new JSONObject(fileContent);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
JSONArray ja = jobj.getJSONArray("ORDERDETAILS");
listContent.add("ITEM NAME");
listContent.add("ORDER QUANTITY");
listContent.add("PRICE");
listContent.add("PREFERED SHOP");
for(int i=0; i<ja.length(); i++){
String ORDERDETAILS = ja.getString(i);
System.out.println(ORDERDETAILS);
jobj1 = new JSONObject(ORDERDETAILS);
String ITEMNAME = jobj1.getString("ITEMNAME");
listContent.add(ITEMNAME);
System.out.println(ITEMNAME);
String ORDERQUANTITY = jobj1.getString("ORDERQUANTITY");
listContent.add(ORDERQUANTITY);
System.out.println(ORDERQUANTITY);
String PRICE = jobj1.getString("PRICE");
listContent.add(PRICE);
System.out.println(PRICE);
String SHOPNAME = jobj1.getString("SHOPNAME");
System.out.println(SHOPNAME);
listContent.add(SHOPNAME);
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
gridView = (GridView)findViewById(R.id.order_details_grid);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,listContent);
gridView.setAdapter(adapter);
// gridView.setAdapter(new ImageAdapter(this));
}
这是我从中获取 json 数据的 json 文件:
{
"ORDERDETAILS": [
{
"ITEMNAME": "APPLE-LAPTOP",
"ORDERQUANTITY": "35",
"PRICE": "4500 DHMS",
"SHOPNAME": "Indico Icon Kits 403",
"SHOPIMAGEURL": "http://s0.geograph.org.uk/photos/43/03/430378_cc40fae8.jpg"
},
{
"ITEMNAME": "ASUS-NOTEBOOK",
"ORDERQUANTITY": "35",
"PRICE": "3500 DHMS",
"SHOPNAME": "Indico Icon Kits 403",
"SHOPIMAGEURL": ""
},
{
"ITEMNAME": "DELL-LAPTOP",
"ORDERQUANTITY": "35",
"PRICE": "9500 DHMS",
"SHOPNAME": "Indico Icon Kits 403",
"SHOPIMAGEURL": ""
}
]
}