【发布时间】:2018-09-10 12:16:17
【问题描述】:
当我获取地震时间时,我正在使用地震 API,它不是人类可读的,如下所示。
API 中的时间为长格式,我想在列表视图中将其显示为 00/00/0000
这就是我从 API 获取时间和更多数据的方式:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(ListView)findViewById(R.id.list_id);
queue= Volley.newRequestQueue(this);
request();
l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ApiClass apiClass=list.get(position);
Uri uri=Uri.parse(apiClass.getUrl());
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
});
}
@Override
public void onResponse(String response) {
try {
JSONObject ob1=new JSONObject(response);
JSONArray ob2=ob1.getJSONArray("features");
for (int i=0;i<ob2.length();i++){
JSONObject ob3=ob2.getJSONObject(i);
JSONObject ob4=ob3.getJSONObject("properties");
String title=ob4.getString("title");
Double mag=ob4.getDouble("mag");
Long time=ob4.getLong("time");
String u=ob4.getString("url");
list.add(new ApiClass(title,mag,time,u));
}
CustomAdapter adapter=new CustomAdapter(MainActivity.this,list);
l.setAdapter(adapter);
}
catch (JSONException e) {
e.printStackTrace();
}
// Display the first 500 characters of the response string.
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
}
});
queue.add(stringRequest);
}
}
以下是我的自定义适配器 java 类:
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View v=convertView;
if(convertView==null){
v= LayoutInflater.from(c).inflate(R.layout.custom_layout,parent,false);
}
ApiClass ob=(ApiClass) arrayList.get(position);
CircleImageView image=v.findViewById(R.id.image_id);
TextView tv1=v.findViewById(R.id.title_id);
TextView tv2=v.findViewById(R.id.magnitude_id);
TextView tv3=v.findViewById(R.id.time_id);
tv1.setText(ob.getTitle().toString());
tv2.setText(ob.getMag().toString());
tv3.setText(ob.getTime().toString());
image.setImageResource(R.drawable.mouse);
return v;
}
这是我的 Apiclass,它返回值以在列表视图中设置值:
public ApiClass(String title, Double mag, Long time, String url) {
this.title = title;
this.mag = mag;
this.time = time;
this.url = url;
}
public String getTitle() {
return title;
}
public Double getMag() {
return mag;
}
public Long getTime() {
return time;
}
public String getUrl() {
return url;
}
}
【问题讨论】:
-
时间是这样的:1737394537378
-
只显示数字
-
上面的长整数类型时间是我得到的一个例子,它不正确
-
我得到的时间与上面的长整数格式相同
标签: android android-api-levels