自定義adapter完成列表的顯示

  • 任務要求
    使用ListView和自定義Adapter完成列表信息的顯示

-思路
1先在Activity中靜態加載一個fragment,在fragment裏面操做
2在fragment中添加ListView控件
3圖例所示的listView模板還要本身從新定義 因此要建立一個XML文件自定義佈局 並在java中添加到listView中
4 這裏我是按照要求自定義了一個Baseadapter類來傳輸數據到前端。
5爲了使代碼更簡潔 我把姓名,年齡,郵箱地址等要信息打包成一個Message類,這樣不用重複寫代碼javascript

XML文件分三個:
1 main_activity(fragment部分)前端

<fragment
        android:id="@+id/list_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.administrator.deadline5_2.ListFragment"
        tools:layout="@layout/fragment_list"/>

2 fragment_list(ListView部分)java

<ListView
        android:id="@+id/list_item1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

3 list_itemandroid

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/txt_name"
            android:hint="@string/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/txt_age"
            android:hint="@string/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/txt_email"
            android:hint="@string/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/txt_address"
            android:hint="@string/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

java文件ide

1 Message.java佈局

public class Message {
    private String name;
    private String age;
    private String email;
    private String address;

     Message(String name,String age,String email,String address) {
        this.name = "姓名: "+name;
        this.age = "年齡: "+age;
        this.email = "郵箱: "+email;
        this.address = "地址: "+address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public String getName(){
        return name;
    }

    public String getAge(){
        return age;
    }

    public String getEmail(){
        return email;
    }

    public String getAddress(){
        return address;
    }

}

2 Messageadapter.javathis

public class MessageAdapter extends BaseAdapter {
    private ArrayList<Message> mData;
    private Context context;

    MessageAdapter(ArrayList<Message> mData, Context context) {
        this.mData = mData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mData.size();//決定了現實多少行ListView
    }
    //根據ListView所在位置返回view
    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view ==null)
        {
            view = LayoutInflater.from(context).inflate(R.layout.list_item,viewGroup,false);
        }
        //獲得view中的控件
        TextView txt_name = (TextView)view.findViewById(R.id.txt_name);
        TextView txt_age = (TextView)view.findViewById(R.id.txt_age);
        TextView txt_email = (TextView)view.findViewById(R.id.txt_email);
        TextView txt_address = (TextView)view.findViewById(R.id.txt_address);

        txt_name.setText(mData.get(i).getName());
        txt_age.setText(mData.get(i).getAge());
        txt_email.setText(mData.get(i).getEmail());
        txt_address.setText(mData.get(i).getAddress());

        return view ;
    }
}

3ListFragment.java指針

public class ListFragment extends Fragment {
    private List<Message> mData ;
    private Context context;
    private MessageAdapter mAdapter;
    private ListView list_item1;
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        context = getActivity();
        mData = new ArrayList<>();
        mData.add(new Message("蔡志坤","25","ffczk86@gmail.com","廈門市"));
        mData.add(new Message("李傑華","25","aa@bb.com","漳州市"));
        mData.add(new Message("張亮","25","cc@gmail.com","廈門市"));
        mData.add(new Message("陳旭","25","ccadd@gmail.com","廈門市"));
        mData.add(new Message("劉玄德","25","ffczk@gmail.com","福州市"));
        mAdapter = new MessageAdapter((ArrayList<Message>)mData,context);
        list_item1 = (ListView)view.findViewById(R.id.list_item1);
        list_item1.setAdapter(mAdapter);

        return view;
    }
}

須要注意的是在寫BaseAdapter時必定要重寫getCount方法和getView方法
在getView方法中先把layout 轉換成view 在獲取裏面的控件 並setText。
在ListFragment。java中設置數據建立適配器傳到前端時必定要按照步驟來 不然在運行是會出現空指針異常code

完成效果圖
blog

相關文章
相關標籤/搜索