Android的ListView控件简单使用

效果图:

页面布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Layout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_startInventory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:background="@drawable/button_style_org"
            android:text="@string/btn_startInventory"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_stopInventory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/button_style_org"
            android:text="@string/btn_stopInventory"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_paraInventory"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/button_style_org"
            android:text="@string/btn_paraInventory"
            android:textColor="@color/white"
            android:textSize="15sp" />

        <Button
            android:id="@+id/btn_clearList"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@drawable/button_style_org"
            android:text="@string/btn_clearList"
            android:textColor="@color/white"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/list_inventory"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_inventoryInfo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/tv_inventoryInfo"
            android:textColor="#0000FF"
            android:textSize="15sp" />

        <include
            android:id="@+id/inventorylist_title"
            layout="@layout/inventorylist_tittle" />

        <ListView
            android:id="@+id/list_inventory_record"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/list_bg"
            android:cacheColorHint="@android:color/transparent"
            android:divider="#f9b68b"
            android:dividerHeight="1.0dp"
            android:fadingEdge="none"
            android:scrollbars="vertical"></ListView>
    </LinearLayout>
</LinearLayout>

LIstView控件的数据布局文件:list_item.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="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/TextViewTag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:background="@drawable/list_bg"
        android:text="E004123456789"
        android:textAlignment="center"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/TextViewNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:background="@drawable/list_bg"
        android:text="1000"
        android:textAlignment="center"
        android:textStyle="bold" />
</LinearLayout>

使用:

新建适配器:

代码 如下:

    //ListView适配器
    private class ReaderAdapter extends BaseAdapter{

        private Context context;
        private LayoutInflater inflater;
        public Hashtable ht;//存储卡号与次数
        public ArrayList<ISOTag> arr;
        public int countTag=0;
        public int countNumber=0;
        public ReaderAdapter(Context context){
            super();
            this.context=context;
            inflater=LayoutInflater.from(context);
            ht=new Hashtable();//存储卡号与次数
            arr = new ArrayList<ISOTag>();
            countTag=0;
            countNumber=0;


        }

        @Override
        public int getCount() {
            return arr.size();
        }

        @Override
        public Object getItem(int i) {
            return i;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public synchronized View getView(final int position, View parentView, ViewGroup viewGroup) {

            if(parentView==null){

                parentView=inflater.inflate(R.layout.list_item,null);

            }

            //卡号
            final TextView textViewTag=(TextView)parentView.findViewById(R.id.TextViewTag);
            textViewTag.setText(arr.get(position).getTag());
            textViewTag.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(arr.size()>0){
                        ISOTag tag=new ISOTag();
                        tag.setTag(textViewTag.getText().toString());
                        arr.set(position,tag);
                    }
                }
            });

            //次数
            final TextView TextViewNumber=(TextView)parentView.findViewById(R.id.TextViewNumber);

            TextViewNumber.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if(arr.size()>0){
                        ISOTag tag=new ISOTag();
                       int num=Integer.parseInt(TextViewNumber.getText().toString());
                      num=num+1;
                        tag.setTagNumber(num);
                        arr.set(position,tag);
                    }
                }
            });

          // textViewTag.setText(arr.get(position).getTag());
           //textViewCount.setText(arr.get(position).getTagNumber());
           // Log.d("Test1",arr.get(position).getTag());

            //更新统计
           // tv_inventoryInfo.setText("标签:"+countTag+" ;总次数:"+countNumber);
            return parentView;
        }


    }

使用适配器:

ListView  list_inventory_record=(ListView)findViewById(R.id.list_inventory_record);

        //添加适配器
   ReaderAdapter    readerAdapter=new ReaderAdapter(this);
       list_inventory_record.setAdapter(readerAdapter);

往适配器中添加数据:

  ISOTag tag=new ISOTag();
                   tag.setTag(((ISO15693Tag )tagList.get(i)).TagID);

                   readerAdapter.arr.add(tag);

简单类:

package com.SerialPort_test;

public class ISOTag {

    private String id;
    private String tag;//卡号
    private int tagNumber;//次数

    public ISOTag() {
    }

    public ISOTag(String tag, int tagNumber) {
        this.tag = tag;
        this.tagNumber = tagNumber;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public int getTagNumber() {
        return tagNumber;
    }

    public void setTagNumber(int tagNumber) {
        this.tagNumber = tagNumber;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

试行

祝您生活愉快!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值