将 JSON 对象放入列表视图 android

Put JSON Object in Listview android

我在 Android Studio 中开发了一个程序,我在其中尝试将 JSON 对象中的项目放入 Listview 中,但它不起作用,而且我没有收到任何错误。 这是 JSON 对象:

{
  "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
}

这是终端状态 class:

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

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

}

这里是终端 class:

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

我的适配器class:

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

和主要的 class 我有一个列表视图,我不会在其中显示 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();
 }
    }

我哪里错了?谢谢。

添加字符串请尝试:

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

希望这个字符串可以证明解析器是安全的。

我找到了我的问题的解决方案,我对代码做了一些更改,但最重要的部分是这一行,适配器中缺少它 class (TerminalStatusAdapter):

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