我试图通过为现有ListView提供可扩展ListView来扩展我的应用程序。我发现的教程/答案通常没有很好的文档记录,也没有检查数据是动态的情况(在我的例子中是来自API的已解析的JSON数组响应)。
我有两个数据类:SearchItem
和InventoryItem
。我用Volley调用了API,JSON看起来像这样:
[
{"product":"foobar", "image_url":"someurl", "inventory_items":
[
{"some_attribute":"some_value"},
{"some_attribute":"some_value"},
{"some_attribute":"some_value"}
]
},
{"product.......
}
]
到目前为止,我只解析了SearchItems
的JSON:
private void parseJsonFeed(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject searchObj = (JSONObject) response.get(i);
SearchItem searchItem = new SearchItem();
searchItem.setId(searchObj.getInt("id"));
searchItem.setProduct(searchObj.getString("product"));
searchItem.setItemImg(searchObj.getString("img"));
searchItem.setId(searchObj.getInt("id"));
searchItem.setUpc(searchObj.getInt("upc"));
searchItems.add(searchItem);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
如何在我的适配器中为每个InvetoryItems
添加子SearchItem
?在解析响应时,我可以遍历每个InventoryItem
,但是如何将其添加为相应SearchItem
的子元素?以下是我现有的BaseAdapter扩展:
public class SearchItemAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<SearchItem> searchItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public SearchItemAdapter(Activity activity, List<SearchItem> searchItems) {
this.activity = activity;
this.searchItems = searchItems;
}
@Override
public int getCount() {
return searchItems.size();
}
@Override
public Object getItem(int location) {
return searchItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.search_item, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
TextView product = (TextView) convertView.findViewById(R.id.product);
TextView upc = (TextView) convertView.findViewById(R.id.upc);
NetworkImageView itemImg = (NetworkImageView) convertView
.findViewById(R.id.itemImg);
SearchItem searchItem = searchItems.get(position);
// product info
product.setText(searchItem.getProduct());
// item pic
itemImg.setImageUrl(searchItem.getItemImg(), imageLoader);
upc.setText(String.valueOf(searchItem.getUpc()));
return convertView;
}
}
答案 0 :(得分:0)
第一部分解析 - 您可以编辑SearchItem
代码来获取和设置List<InventoryItem>
然后在您的解析方法中,您可以遍历jsonArray searchObj.getJsonArray("inventory_item")
创建inventoryItem
对象,将其添加到inventoryList
,以后可以添加到searchItem
。
为InventoryItem创建一个单独的pojo类,然后编辑SearchItem
类
public class SearchItem{
private List<InventoryItem> inventoryList;
public void setInventoryList(List<InventoryList> inventoryList)
{
this.inventoryList = inventoryList;
}
public List<InventoryItem> getInventoryList()
{
return this.inventoryList;
}
//other object and get/set
}
private void parseJsonFeed(JSONArray response) {
try {
for (int i = 0; i < response.length(); i++) {
JSONObject searchObj = (JSONObject) response.get(i);
SearchItem searchItem = new SearchItem();
searchItem.setId(searchObj.getInt("id"));
searchItem.setProduct(searchObj.getString("product"));
searchItem.setItemImg(searchObj.getString("img"));
searchItem.setId(searchObj.getInt("id"));
searchItem.setUpc(searchObj.getInt("upc"));
if(searchObj.has("inventory_item")
{
JsonArray array = searchObj.getJsonArray("inventory_item");
List<InventoryItem> inList = new ArrayList<InventoryItem>();
for(int k=0;k<inList.length();k++)
{
InventoryItem item = new InventoryItem();
item.setSomeAttribute(array.get(k).getString("some_attribute"));
inList.add(item);
}
searchItem.setInventoryList(inList);
}
searchItems.add(searchItem);
}
// notify data changes to list adapater
listAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
第二部分:使用ExpandableListAdapter
代替BaseAdapter
。从searchItems.get(groupPosition).getInventoryList().size()
getChildrenCount()
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<SearchItem> list;
public ExpandableListAdapter(Context context, List<SearchItem> list) {
this._context = context;
this.list = list;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.list.get(groupPosition).getInventoryList()
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final InventoryItem item = (InventoryItem) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(item.getSomeAttribute());
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.list.get(groupPosition).getInventoryList()
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.list.get(groupPosition);
}
@Override
public int getGroupCount() {
return this.list.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
SearchItem item = (SearchItem) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(item.getTitle());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 1 :(得分:0)
您可以使用将所有json解析为对象的库http://jackson.codehaus.org/。
要解析你的json,请尝试使用此代码...响应是你的JSON。
ObjectMapper mapper = new ObjectMapper();
try {
List< SearchItem > search = mapper
.readValue(
response,
new TypeReference<List< SearchItem >>() {
});
//TODO save your list.
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
你的对象SearchItem
@JsonIgnoreProperties(ignoreUnknown = true)
公共类SearchItem {
public String product;
public String image_url;
public List<Inventario> inventory_items;
public static class Inventario {
public String Attribute1;
public String Attribute2;
}
}
这可以帮助你解析你的json。
要显示,您需要使用BaseExapter的BaseExpandableListAdapter项目。