通过http获取数据,反序列化成List<Map<String, String>>对象。向主线程发送消息。
void getDatas( ){ try{ commHttpUtils.doGetAsyn(dateUrl, new CallBack() { @Override public void onRequestComplete(String result) { List<Map<String, String>> items = new ArrayList<Map<String, String>>(); items = ( new Gson()).fromJson(result , items.getClass()); if( msgHandler!=null){ try{ Message msg = new Message(); msg.what = 1000; msg.obj = items; msgHandler.sendMessage(msg); } catch (Exception eer){ Log.e(TAG, "onRequestComplete: "+ eer.getMessage() ); } } } }); } catch(Exception er){ Log.e("videoList error.", er.getMessage() ); } }
新建一个layout,内容为列表中的一行的显示内容(videolist_adapteritem.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dp"> <TextView android:id="@+id/txt_listvideo_item_caption" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="..."></TextView> </LinearLayout>
这下是关键,将数据映射到视图的上.
class vidoAdapter extends ArrayAdapter<Map<String, String>> { int resourceId ; final List<Map<String, String>> dataList ; public vidoAdapter(@NonNull Context context, int resource, @NonNull List<Map<String, String>> objects) { super(context, resource ,objects); resourceId = resource; dataList = objects; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view = null; TVideoControl videoControl; try{ Map<String, String> dataInfo = dataList.get(position); if( convertView==null){ view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false); videoControl = new TVideoControl(); videoControl.txtCaption =view.findViewById(R.id.txt_listvideo_item_caption); view.setTag(videoControl); } else{ view = convertView; videoControl = (TVideoControl) view.getTag(); } videoControl.txtCaption.setText(dataInfo.get("Caption")); } catch (Exception er){ Log.e(TAG, "getView: "+er.getMessage() ); } return view; } class TVideoControl{ TextView txtCaption; } }
void updateList(final List<Map<String, String>> dataItems){ vidoAdapter adapter = new vidoAdapter(this.getContext() , R.layout.videolist_adapteritem, dataItems ); ListView lst = FragmentView.findViewById(R.id.listViewVideoList); lst.setAdapter(adapter); lst.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Map<String, String> info = dataItems.get(position); doPlayVideo(info); } }); }