您的位置:首页 > 移动开发 > Android开发

Android 界面ListView使用

2016-01-26 09:53 337 查看
不多写文字解释了,基本用到的源码到写到了用心看看就行。





界面.xml

<ListView 

      android:id="@+id/listview"

      android:layout_width="match_parent"

      android:layout_height="wrap_content"

 ></ListView>

XXXActivity.java

List list=new List();

list.add(Fpyj_jc );

……



Map<Integer, String> viewIdPropPair = new LinkedHashMap<Integer, String>(5);
viewIdPropPair.put(R.id.fp_yj_e3, "fenshu");
viewIdPropPair.put(R.id.fp_yj_e4, "qishihaoma");

……
lv.setAdapter(new BeanAdapter<Fpyj_jc >(XXXActivity.this, R.layout.XXX_item,
list(这里是集合),
viewIdPropPair));

BeanAdapter.java

package com.nsrsb;

import java.lang.reflect.Field;

import java.util.List;

import java.util.Map;

import com.tax.client.Fxtx;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.EditText;

public class BeanAdapter<A> extends BaseAdapter {
private Context context;
private List<A> list;
private LayoutInflater inflater;
private int layoutResId;
private Map<Integer, String> viewIdPropPair;

public BeanAdapter(Context context2, int id, List<A> list2,
Map<Integer, String> map) {
this.context = context2;
this.layoutResId = id;
this.list = list2;
this.viewIdPropPair = map;
inflater = (LayoutInflater) context2
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

@Override
public Object getItem(int position) {
return list.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = inflater.inflate(this.layoutResId, null);
A bean = list.get(position);
try {
for (Map.Entry<Integer, String> entry : viewIdPropPair.entrySet()) {
EditText ed = (EditText) layout.findViewById(entry.getKey());
String str = String.valueOf(getBeanProperty(bean,
entry.getValue()));
ed.setText(str);
}
} catch (Exception e) {
e.printStackTrace();
}
return layout;
}

private Object getBeanProperty(A bean, String propertyName)
throws NoSuchFieldException, IllegalAccessException {
Field field = bean.getClass().getDeclaredField(propertyName);
field.setAccessible(true);
return field.get(bean);
}

}

XXX_item.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#faf7f9" >

    <LinearLayout

        android:id="@+id/fp_yj_jc_list"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:background="#ffffff"

        android:orientation="vertical" >

        <TextView

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="15dp"

            android:layout_marginRight="15dp"

            android:layout_marginTop="10dp"

            android:text="份数"

            android:textColor="#3d4245"

            android:textSize="15sp" />

        <EditText

            android:id="@+id/fp_yj_e3"

            android:layout_width="match_parent"

            android:layout_height="38dp"

            android:layout_marginLeft="15dp"

            android:layout_marginRight="15dp"

            android:layout_marginTop="6dp"

            android:background="@drawable/ed_biankuang_yuanjiao"

            android:digits="0123456789"

            android:enabled="false"

            android:hint="@null"

            android:inputType="numberDecimal"

            android:paddingLeft="15dp"

            android:textColor="#3d4245"

            android:textSize="15sp" />

        <TextView

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="15dp"

            android:layout_marginRight="15dp"

            android:layout_marginTop="10dp"

            android:text="发票起始号码"

            android:textColor="#3d4245"

            android:textSize="15sp" />

        <EditText

            android:id="@+id/fp_yj_e4"

            android:layout_width="match_parent"

            android:layout_height="38dp"

            android:layout_marginLeft="15dp"

            android:layout_marginRight="15dp"

            android:layout_marginTop="6dp"

            android:background="@drawable/ed_biankuang_yuanjiao"

            android:enabled="false"

            android:hint="@null"

            android:inputType="numberDecimal"

            android:paddingLeft="15dp"

            android:textColor="#3d4245"

            android:textSize="15sp" />

        <TextView

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_marginLeft="15dp"

            android:layout_marginRight="15dp"

            android:layout_marginTop="10dp"

            android:text="发票结束号码"

            android:textColor="#3d4245"

            android:textSize="15sp" />

        <EditText

            android:id="@+id/fp_yj_e5"

            android:layout_width="match_parent"

            android:layout_height="38dp"

            android:layout_marginBottom="10dp"

            android:layout_marginLeft="15dp"

            android:layout_marginRight="15dp"

            android:layout_marginTop="6dp"

            android:background="@drawable/ed_biankuang_yuanjiao"

            android:enabled="false"

            android:hint="@null"

            android:inputType="numberDecimal"

            android:paddingLeft="15dp"

            android:textColor="#3d4245"

            android:textSize="15sp" />

    </LinearLayout>

</RelativeLayout>

Fpyj_jc .java

package com.tax.client;

public class Fpyj_jc {
String fenshu;
String qishihaoma;
String jieshuhaoma;

public String getFenshu() {
return fenshu;
}

public void setFenshu(String fenshu) {
this.fenshu = fenshu;
}

public String getQishihaoma() {
return qishihaoma;
}

public void setQishihaoma(String qishihaoma) {
this.qishihaoma = qishihaoma;
}

public String getJieshuhaoma() {
return jieshuhaoma;
}

public void setJieshuhaoma(String jieshuhaoma) {
this.jieshuhaoma = jieshuhaoma;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: