我在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();
}
}
我在哪裏錯了?謝謝。
你提取終端部分「s」併發送到該方法? 檢查「s」字符串中的值。 – sumandas
拜託,你能更明確嗎? – ruru
當然,ApiResponse resp = new ApiResponse (s,TerminalStatus.class); 行使用變量s,檢查這個變量的值。 –
sumandas