安卓從入門到放棄(四) 簡單的自定義Listview

 

一、新建數據類

package com.example.lch;

public class IGUInfo {
    private long sn;
    private int imageId;

    public long getSn() {
        return sn;
    }

    public void setSn(long sn) {
        this.sn = sn;
    }

    public int getImageId() {
        return imageId;
    }


    public void setImageId(int imageId) {
        this.imageId = imageId;
    }

    public IGUInfo( long sn ,int imageId ){
        this.sn = sn;
        this.imageId = imageId;
    }


}

二、ListItem的佈局

<?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">

    <ImageView
        android:id="@+id/igu_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/igu_sn_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="10dip"
        />


</LinearLayout>

 

三、主佈局增加ListView

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

 

四、增加自定義Adapter

package com.example.lch;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class IGUInfoListAdapter extends ArrayAdapter<IGUInfo> {

    private  int resourceId;
    public IGUInfoListAdapter(Context context, int textViewResourceId, List<IGUInfo> object){
        super(context,textViewResourceId,object);
        resourceId = textViewResourceId;
    }


    public View getView(int position, View converView , ViewGroup parent){
        IGUInfo info = getItem(position);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.igu_info_item,null);
        ImageView imageView = view.findViewById(R.id.igu_image);
        TextView textView = view.findViewById(R.id.igu_sn_text);
        imageView.setImageResource(info.getImageId());
        textView.setText(String.valueOf(info.getSn()));
        return view;
    }

}

 

五、MainActivity 創建,數據填充

package com.example.lch;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private List<IGUInfo> IGU_list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFruits();
        IGUInfoListAdapter adapter = new IGUInfoListAdapter(this,R.layout.igu_info_item ,IGU_list);
        ListView listView = findViewById(R.id.iguInfoListView);
        listView.setAdapter(adapter);

    }

    private void initFruits() {
        IGUInfo apple = new IGUInfo(350000001, R.mipmap.igu_image);
        IGU_list.add(apple);
        IGUInfo banana = new IGUInfo(350000002, R.mipmap.igu_image);
        IGU_list.add(banana);

    }
}

 

六、展示

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章