0

I develop a program in Android Studio, in which i try to put item from JSON Object into a Listview and it does not work and i do not receive any errors. Here is JSON Object:

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

Here is the TerminalStatus class:

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

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

}

Here is Terminal 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;
    }
}

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

and the main class where i have a Listview in which i wont to display content of JSON Object:

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

Where am i wrong? Thanks.

ruru
  • 47
  • 1
  • 10
  • Are you extracting the terminals part "s" and sending that to the method? check the value in "s" string. – sumandas Aug 17 '16 at 19:28
  • Please, can you be more explicit? – ruru Aug 17 '16 at 19:40
  • Sure,ApiResponse resp = new ApiResponse(s, TerminalStatus.class); line uses a variable s, check the value of this variable. – sumandas Aug 17 '16 at 19:41
  • Variable s has no value. – ruru Aug 17 '16 at 19:53
  • So now do a favor, force the variable s to contain your json string starting from {"terminals": [ { till end of the bracket for terminal} and see if it works. Idea is to see if parser has issue or string sent has issues. – sumandas Aug 17 '16 at 19:56
  • Or I do not know to how to do this, or not working. I initialize s = "{ "terminals": [ { "terminal": "terminal1", "registered": true, "ip": "123456789", "user_agent": "X-Lite " }, { "terminal": "terminal2", "registered": false }, { "terminal": "terminal3", "registered": false }, { "terminal": "terminal4", "registered": false } ] }"; and s still have no value. – ruru Aug 17 '16 at 20:39
  • Added the string in answer as it shows with formatting, use it from there. – sumandas Aug 17 '16 at 20:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121208/discussion-between-sumandas-and-ruru). – sumandas Aug 17 '16 at 20:58

2 Answers2

0

Adding the string please try it:

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

Hope this string can work to prove parser for that is safe.

sumandas
  • 555
  • 7
  • 20
0

I found the solution of my problem, i did few changes of my code, but the most important part is this line, which was missing from Adapter class (TerminalStatusAdapter):

 super(context, R.layout.terminal_status_row, terminalStatus.getTerminals());
ruru
  • 47
  • 1
  • 10