2016-08-17 67 views
0

我在Android Studio中開發了一個程序,在該程序中,我嘗試將JSON對象中的項目放入Listview中,但它不起作用,我也沒有收到任何錯誤。 這裏是JSON對象:在ListView中放置JSON對象android

{ 
    "success": true, 
    "body": { 
    "terminals": [ 
     { 
     "terminal": "terminal1", 
     "registered": true, 
     "ip": "123456789", 
     "user_agent": "X-Lite " 
     }, 
     { 
     "terminal": "terminal2", 
     "registered": false 
     }, 
     { 
     "terminal": "terminal3", 
     "registered": false 
     }, 
     { 
     "terminal": "terminal4", 
     "registered": false 
     } 
    ] 
    }, 
    "error": null 
} 

這裏是TerminalStatus類:

public class TerminalStatus { 
    @JsonProperty("terminals") private List<Terminal> terminals; 

    public List<Terminal> getTerminals(){return terminals;} 

} 

這裏是終端類:

public class Terminal { 
    @JsonProperty("terminal") public String terminal; 
    @JsonProperty("registered") public boolean registeredTerminals; 
    @JsonProperty("ip") public String ip; 
    @JsonProperty("user_agent") public String userAgent; 

    public String getTerminal(){ 
     return terminal; 
    } 

    public Boolean getRegisteredTerminals(){ 
     return registeredTerminals; 
    } 

    public String getIp(){ 
     return ip; 
    } 

    public String getUserAgent(){ 
     return userAgent; 
    } 
} 

我的適配器類:

public class TerminalStatusAdapter extends ArrayAdapter<Terminal> { 

    private TerminalHolder holder; 
    private Context context; 
    private List<Terminal> items; 

    public TerminalStatusAdapter(Context context, List<Terminal> items) { 
     super(context, R.layout.terminal_status_row); 
     this.context = context; 
     this.items = items; 
    } 

/* 
    @Override 
    public int getCount() { 
     if(items == null) { 
      items = new Terminal(); 
     } 
     return items; 
    }*/ 

    @Override 
    public Terminal getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //final Terminal item = items.get(position); 
     holder = null; 
     if(convertView == null){ 
      convertView = LayoutInflater.from(this.context).inflate(R.layout.terminal_status_row, parent, false); 
      holder = new TerminalHolder(); 
      holder.txtTerminalStatus = (TextView) convertView.findViewById(R.id.txt_terminal_status); 
      holder.txtRegisteredTermninal = (TextView)convertView.findViewById(R.id.txt_registered_status); 
      holder.txtIp = (TextView)convertView.findViewById(R.id.txt_ip); 
      holder.txtUserAgent = (TextView)convertView.findViewById(R.id.txt_user_agent); 
      convertView.setTag(holder); 
     }else { 
      holder = (TerminalHolder) convertView.getTag(); 
     } 
     holder.txtTerminalStatus.setText(items.get(position).getTerminal()); 
     holder.txtRegisteredTermninal.setText(items.get(position).getRegisteredTerminals().toString()); 
     holder.txtIp.setText(items.get(position).getIp()); 
     holder.txtUserAgent.setText(items.get(position).getUserAgent()); 

     return convertView; 
    } 

    private class TerminalHolder { 

     public TextView txtTerminalStatus; 
     public TextView txtRegisteredTermninal; 
     public TextView txtIp; 
     public TextView txtUserAgent; 
    } 
} 

和邁n類在哪裏我有一個列表視圖,其中我不會顯示JSON對象的內容:

public class CallTerminal extends Fragment{ 
     private ListView terminalStatusRegistered; 
     private TerminalStatusAdapter terminalStatusAdapter; 
     public ArrayList<Terminal> terminalRegisteredData = null; 

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View v = inflater.inflate(vm.voicemail.app.R.layout.call_terminal_fragment, container, false); 
      terminalStatusRegistered = (ListView)v.findViewById(R.id.list_registered_terminals); 
    return v; 
    } 
     try { 
          ApiResponse<TerminalStatus> resp = new ApiResponse<TerminalStatus>(s, TerminalStatus.class); 
    try { 
     terminalStatus = resp.getObject(); 
     terminalRegisteredData = new ArrayList<Terminal>(); 
     terminalStatusAdapter = new TerminalStatusAdapter(getContext(), terminalRegisteredData);      terminalStatusRegistered.setAdapter(terminalStatusAdapter); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } catch (IOException e) { 
    e.printStackTrace(); 
} 
    } 

我在哪裏錯了?謝謝。

+0

你提取終端部分「s」併發送到該方法? 檢查「s」字符串中的值。 – sumandas

+0

拜託,你能更明確嗎? – ruru

+0

當然,ApiResponse resp = new ApiResponse (s,TerminalStatus.class); 行使用變量s,檢查這個變量的值。 – sumandas

回答

0

,我發現我的問題的解決方案,我做我的代碼一些變化,但最重要的是這條線,這是從Adapter類失蹤(TerminalStatusAdapter):

super(context, R.layout.terminal_status_row, terminalStatus.getTerminals()); 
0

添加字符串,請嘗試:

String s = " {\r\n" + 
     " \"terminals\": [\r\n" + 
     "  {\r\n" + 
     "  \"terminal\": \"terminal1\",\r\n" + 
     "  \"registered\": true,\r\n" + 
     "  \"ip\": \"123456789\",\r\n" + 
     "  \"user_agent\": \"X-Lite \"\r\n" + 
     "  },\r\n" + 
     "  {\r\n" + 
     "  \"terminal\": \"terminal2\",\r\n" + 
     "  \"registered\": false\r\n" + 
     "  \"ip\": \"123456789\",\r\n" + 
     "  \"user_agent\": \"X-Lite \"\r\n" + 
     "  },\r\n" + 
     "  {\r\n" + 
     "  \"terminal\": \"terminal3\",\r\n" + 
     "  \"registered\": false\r\n" + 
     "  \"ip\": \"123456789\",\r\n" + 
     "  \"user_agent\": \"X-Lite \"\r\n" + 
     "  },\r\n" + 
     "  {\r\n" + 
     "  \"terminal\": \"terminal4\",\r\n" + 
     "  \"registered\": false\r\n" + 
     "  \"ip\": \"123456789\",\r\n" + 
     "  \"user_agent\": \"X-Lite \"\r\n" + 
     "  }\r\n" + 
     " ]\r\n" + 
     " }"; 

希望這個字符串可以工作,證明解析器是安全的。

+0

對不起,它還沒有價值。 – ruru

+0

你的字符串「s」不包含值或resp變量? – sumandas

+0

等待看起來是json,終端結構不一樣,底部條目的ip和user_agent在哪裏 – sumandas