Android 界面ListView使用

不多寫文字解釋了,基本用到的源碼到寫到了用心看看就行。



界面.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;
}
}

發佈了54 篇原創文章 · 獲贊 30 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章