只想从JSON数组中提取特定的对象细节

时间:2016-03-16 09:34:09

标签: android json listview

自定义适配器类

public class CustomListViewAdapter extends ArrayAdapter<Details> {

Context context;
String vid = "Video";

public CustomListViewAdapter(Context context, int resourceId,
                             List<Details> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    //ImageView textView;
    TextView txtTitle;
    TextView txtTitle2;
    TextView txtDesc;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    //Details rowItem = (Details) getItem(position);
    Details rowItem = getItem(position);
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null || convertView.getTag() == null) {

        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.list_item, null); //TODO: parent instead of null?
        holder = new ViewHolder();

        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        holder.txtTitle2 = (TextView) convertView.findViewById(R.id.link);
        convertView.setTag(holder);
        convertView.setTag(holder);

    }

        else if (rowItem.getResType().toString().equals(vid)){

        holder = (ViewHolder) convertView.getTag();
    }

        holder.txtDesc.setText(rowItem.getResType());
        holder.txtTitle.setText(rowItem.getName());
        holder.txtTitle2.setText(rowItem.getUrl());


        // holder.textView.setImageResource(rowItem.getImageId());

        return convertView;
    }}

获取列表的代码。

详细信息是我的JSON对象的getter和setter类

     ArrayList<Details> details; 

          private void aeroMethod(List<Details> mList) {
         layout = (LinearLayout) findViewById(R.id.linear);


    for (Details bean : details) {

        if (bean.getResType().equals("Videos"))

        //{if (bean.getResType().equalsIgnoreCase("Videos")) {

        {
            //nameList.add(bean.getResType());
            ListView listView = (ListView) findViewById(R.id.list);
            CustomListViewAdapter adapter = new CustomListViewAdapter(this,
                    R.layout.list_item, mList);
            listView.setAdapter(adapter);


        }

    }
}

在adapterI中放入if语句但它不起作用。我的JSON就是这样的

         {  
     "results":[  
       {  
        "id":87,
     "resType":"Notes",
     "resLink":"upload/19/conserving our environment.pdf",
     "resName":"Notes",

  },


  {  
     "id":88,
     "resType":"Videos",
     "resLink":"-ehD4H_ywyQ",
     "resName":"Video",

  },


  {  
     "id":89,
     "resType":"Videos",
     "resLink":"-ehD4H_ywyQQ",
     "resName":"Video",

  }, 

因此,如果resType是Video,我希望视频的所有内容都意味着resLink,resName,id

1 个答案:

答案 0 :(得分:1)

将Json作为字符串读取并使用以下代码: -

  try {

       JSONArray jarray = new JSONArray(json);
        System.out.println(jarray);
        //jObj = new JSONObject(jarray);
        for(int i=0; i<jarray.length(); i++)
        {
            jObj=jarray.getJSONObject(i);
            if(jObj.getString("resType").contains("Videos"){

            Log.d("Item name: ", jObj.getString("id"));
            Log.d("Item name: ", jObj.getString("resType"));
            Log.d("Item name: ", jObj.getString("resLink"));
            Log.d("Item name: ", jObj.getString("resName"));
          }
        }
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }