Android 如何在TableLayout中显示JSON?
如何在表格布局中显示此内容?我想显示一个json文件,如下所示: 在我下面的代码中,网络连接正在工作,它解析图像名称和url并显示它。我只是想知道如何在json文件中添加一些新属性,比如itemquantity 并在我的json解析代码中访问,并以与此图像相同的布局显示 这是我的JSON文件Android 如何在TableLayout中显示JSON?,android,json,tablelayout,Android,Json,Tablelayout,如何在表格布局中显示此内容?我想显示一个json文件,如下所示: 在我下面的代码中,网络连接正在工作,它解析图像名称和url并显示它。我只是想知道如何在json文件中添加一些新属性,比如itemquantity 并在我的json解析代码中访问,并以与此图像相同的布局显示 这是我的JSON文件 { "worldpopulation": [ { "rank":1, "name": "Angelina",
{ "worldpopulation":
[
{
"rank":1,
"name": "Angelina",
"url": "http://www.bounty4u.com/android/images/angie.jpg"
},
{
"rank":2,
"name": "Ashton ",
"url": "http://www.bounty4u.com/android/images/ashton.jpg"
},
{
"rank":3,
"name": "Jackman",
"url": "http://www.bounty4u.com/android/images/hugh.jpg"
}
]
}
这是我的代码:
public class fifthscreen extends Activity {
HorizontalListView listview;
CategoryListAdapter3 cla;
String DescriptionAPI;
static ArrayList<Long> Category_ID = new ArrayList<Long>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fifthscreen);
listview = (HorizontalListView) this.findViewById(R.id.listview2);
cla = new CategoryListAdapter3(fifthscreen.this);
DescriptionAPI = Utils.DescriptionAPI;
// clearData();
try {
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client.getParams(),
15000);
HttpUriRequest request = new HttpGet(DescriptionAPI);
HttpResponse response = client.execute(request);
InputStream atomInputStream =
response.getEntity().getContent();
BufferedReader in = new BufferedReader(new
InputStreamReader(atomInputStream));
String line;
String str = "";
while ((line = in.readLine()) != null){
str += line;
}
JSONObject json = new JSONObject(str);
JSONArray data =
json.getJSONArray("worldpopulation");
for (int i = 0; i < data.length(); i++) {
JSONObject object = data.getJSONObject(i);
// JSONObject category =
object.getJSONObject("Category");
Category_ID.add(Long.parseLong(object.getString("rank")));
Category_name.add(object.getString("name"));
Category_image.add(object.getString("url"));
Log.d("Category name", Category_name.get(i));
listview.setAdapter(cla);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
// IOConnect = 1;
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public class CategoryListAdapter3 extends BaseAdapter {
private Activity activity;
private ImageLoader imageLoader;
public CategoryListAdapter3(Activity act) {
this.activity = act;
imageLoader = new ImageLoader(act);
}
public int getCount() {
// TODO Auto-generated method stub
return fifthscreen.Category_ID.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.viewitem2, null);
holder = new ViewHolder();
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.txtText = (TextView) convertView.findViewById(R.id.title2);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.image2);
holder.txtText.setText(fifthscreen.Category_name.get(position));
//
imageLoader.DisplayImage(Utils.AdminPageURL+CategoryList.Category_image.get(position),
imageLoader.DisplayImage(fifthscreen.Category_image.get(position),
activity, holder.imgThumb);
return convertView;
}
}
公共类第五屏幕扩展活动{
水平视图列表视图;
3-共轭亚基;
字符串描述符API;
静态ArrayList Category_ID=新ArrayList();
静态ArrayList Category_name=新ArrayList();
静态ArrayList Category_image=新ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局第五屏幕);
listview=(HorizontalListView)this.findViewById(R.id.listview2);
cla=新的CategoryListAdapter3(fifthscreen.this);
DescriptionAPI=Utils.DescriptionAPI;
//clearData();
试一试{
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),15000);
HttpConnectionParams.setSoTimeout(client.getParams(),
15000);
HttpUriRequest请求=新的HttpGet(DescriptionAPI);
HttpResponse response=client.execute(请求);
InputStream atomInputStream=
response.getEntity().getContent();
BufferedReader in=新的BufferedReader(新的
InputStreamReader(atomInputStream));
弦线;
字符串str=“”;
而((line=in.readLine())!=null){
str+=直线;
}
JSONObject json=新的JSONObject(str);
JSONArray数据=
getJSONArray(“世界人口”);
对于(int i=0;i
为什么要使用表格布局。与listview相同的功能,而且非常容易使用,因此我建议您使用此功能。它提供了表格布局的多列功能
请查看此使用谷歌gson
Gson是一个Java库,可用于将Java对象转换为其JSON表示形式。它还可用于将JSON字符串转换为等效的Java对象。Gson可以处理任意Java对象,包括您没有源代码的现有对象。
它在php中易于使用json_encode()
请参阅下面的链接
在我的json文件中添加新数组数量项目,并在布局中访问和显示我该怎么做???我不知道如何像这样显示json数据。请添加一些虚拟的“项目”“数量”在我的json文件中,如图所示,在屏幕上显示如图所示,请帮助我不要使用字符串,我正在从json文件中获取数据。我是否愿意在我的json文件中添加一些节点或数组,并如图所示显示它们plzi不知道图像中显示的数据是数组还是json文件中的多个节点我粘贴我的jso上面的n文件只需添加一些新数据,如y json代码中的“item”quantity“access,并显示与此屏幕相同的内容,请回复我的这篇文章