[英]Why can't I update my custom list view from API?
I am trying to populate a custom ListView using data from an API.. the data shows in a list but not in the custom layout format of Title and Subtitle with an icon. 我正在尝试使用来自API的数据填充自定义ListView。数据显示在列表中,但没有以带有图标的Title和Subtitle的自定义布局格式显示。 It currently displays as two TextView items.
当前显示为两个TextView项。 I would like to get the Line name to appear as a row heading and the line status to appear as the row subheading.
我想让行名显示为行标题,行状态显示为行子标题。 A point in the right direction would be very much appreciated.
朝着正确方向的观点将不胜感激。 Please see code below.
请参见下面的代码。
public class MainActivity extends AppCompatActivity {
ListView list;
String[] titles;
String [] description;
int[] imgs = {R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,
R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon,
R.drawable.tube_icon,R.drawable.tube_icon,R.drawable.tube_icon};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////// GET DATA FROM API AND ADDS TO LIST ARRAY
//list view and arrayList
final ListView listView = (ListView) findViewById(list1);
final ArrayList<String> tubeLines = new ArrayList<>();
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tubeLines);
listView.setAdapter(arrayAdapter);
RequestQueue queue = Volley.newRequestQueue(this);
String url ="https://api.tfl.gov.uk/line/mode/tube,overground,dlr,tflrail/status";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray array = new JSONArray(response);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.optJSONObject(i);
String line = object.optString("name");
//nested array
JSONArray arrayStatus = object.getJSONArray("lineStatuses");
int len = arrayStatus.length();
String statusSeverityDescription = null;
for (int j = 0; j < len; j++) {
JSONObject o = arrayStatus.getJSONObject(j);
statusSeverityDescription = o.optString("statusSeverityDescription", "");
}
if (line != null) {
tubeLines.add(line + " "+ statusSeverityDescription);
}
}
// Once we added the string to the array, we notify the arrayAdapter
arrayAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(stringRequest);
}
}
class MyAdapter extends ArrayAdapter<String> {
Context context;
int [] images;
String [] mytitles;
String [] myDescriptions;
MyAdapter(Context c,String[] titles,int imgs[],String[]desc)
{
super(c,R.layout.row,R.id.lineName,titles);
this.context=c;
this.images=imgs;
this.mytitles=titles;
this.myDescriptions=desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row,parent,false);
ImageView myImage = (ImageView) row.findViewById(R.id.lineIcon);
TextView myTitle = (TextView) row.findViewById(R.id.lineName);
TextView myDesc = (TextView) row.findViewById(R.id.lineStatus);
myImage.setImageResource(images[position]);
myTitle.setText(mytitles[position]);
myDesc.setText(myDescriptions[position]);
return row;
}
}
why you are not using Recyclerview instead of listview. 为什么不使用Recyclerview而不是Listview。 Create a custom layout.
创建一个自定义布局。 And use Recyclerview to display your data.
并使用Recyclerview显示您的数据。 This will more efficient.
这样会更有效率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.