简体   繁体   English

为什么我不能通过API更新自定义列表视图?

[英]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.

相关问题 为什么我不能更新视图片段phoneNumberEditText? - Why can't I update my view fragment phoneNumberEditText? 无法更新我的列表视图的数据 - Can't update data for my list view 为什么我似乎无法从oncreate更新gridview? - Why I can't seem to update my gridview from oncreate? 即使我不触摸Android上的视图,为什么仍可以拖动自定义视图 - Why can I still drag my custom view even if I don't touch the view on Android 我无法使用商品ID更新列表视图中的商品。 - I can't update an item on my list view using item id. 我不知道为什么我的列表视图没有按预期更新 - I can't figure out why my list view is not updated as I expect 我无法使用自定义适配器数据清除列表视图? - what i can't clear my list view with custom adapter data? 为什么无法将带有自定义适配器的网格视图导入片段? - why i can't import my grid view with custom adapter into a fragment? SimpleCursorAdapter 在 arguments - Android 中找不到我的自定义列表视图 - SimpleCursorAdapter can't find my custom view of list in arguments - Android 为什么我不能更新表上的外部字段? - Why can't I update a foreign field on my table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM