2016-03-16 66 views
0

定義適配器類只想提取JSON陣列

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; 
    }} 

我的代碼的特定對象的細節得到列表。

細節是我的getter和setter類JSON對象

 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); 


     } 

    } 
} 

在和適配器把if語句,但它不是working.My 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是視頻我希望所有的視頻內容意味着resLink,resName,ID

+0

檢查[這個答案](http://stackoverflow.com/questions/17348453/parsing-youtube-gdata-json-into-listview) –

+0

你可以發佈你在哪裏用JSON數據填充你的列表? –

+0

我填充它們在fragment.So在我的listview中所有的內容都來了。我只想要那些滿足if條件if(bean.getResType()。equals(「Videos」))如上代碼 –

回答

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()); 
    } 
+0

我的問題沒有得到JSON對象,我的問題是隻得到滿足條件的對象。這裏的一些if(bean.getResType()。equals(「Videos」)) –

+0

@ShwetabhSingh,檢查我編輯的答案.. – Laxmeena

+0

編譯時間錯誤是爲了當我使用if(顯示錶達式expectedjObj.getString(「resType」)。contains(「Videos」) –