2. ListView设计步骤

    1. 获取数据

通过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() );

    }

}

 

    1. 建立layout

新建一个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>

 

    1. 设计adapter

这下是关键,将数据映射到视图的上.

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;

    }

}

 

    1. 加载适配器设置单击列表条目事件
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);

        }

    });

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值