iT邦幫忙

2021 iThome 鐵人賽

DAY 29
0
自我挑戰組

創業歷程系列 第 29

企劃實現(29)

使用自定義的listview
第四部:創建listview的adapter

package com.example.graduationtopic;

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

import java.util.List;

public class Adapter_about_class extends BaseAdapter {
    Context context;
    LayoutInflater inflater;
    String[] ar_classname, ar_starttime, ar_endtime, ar_classmate,ar_datetime;
    public Adapter_about_class(Context context, String[] classname,String[] datetime, String[] starttime,  String[] endtime,  String[] classmate){
    //順序會有差要注意
    
    
        this.context=context;


        ar_datetime=datetime;
        ar_starttime= starttime;
        ar_endtime= endtime;
        ar_classmate= classmate;
        ar_classname= classname;
        //你要傳幾個資料就要丟幾個
    }
    @Override
    public int getCount() {
        return ar_classname.length;//只要載入幾次
    }

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

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        TextView ed_classname,ed_starttime,ed_endtime,ed_classmate,ed_datetime;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.list_item, parent, false);
        ed_classname =  itemView.findViewById(R.id.classname);
        ed_starttime = itemView.findViewById(R.id.starttime);
        ed_endtime =  itemView.findViewById(R.id.endtime);
        ed_classmate =  itemView.findViewById(R.id.classmate);
        ed_datetime =  itemView.findViewById(R.id.datetime);


        ed_classname.setText(ar_classname[position]);
        ed_starttime.setText(ar_starttime[position]);
        ed_endtime.setText(ar_endtime[position]);
        ed_classmate.setText(ar_classmate[position]);
        ed_datetime.setText(ar_datetime[position]);
        return itemView ;
    }
}

第五步:在activity引用這個adapter

adapter_about_class = new Adapter_about_class(getActivity(),ar_classname,ar_datetime,ar_starttime,ar_endtime,ar_student);

        aboutclass_listview.setAdapter(adapter_about_class);
        aboutclass_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent=new Intent(getActivity(),Edit_Add_Class.class);
            Bundle bundle = new Bundle(); //new一個Bundle物件,並將要傳遞的資料傳入
            bundle.putInt("i", i+1);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });}
    ```
    

上一篇
企劃實現(28)
下一篇
企劃實現(30)
系列文
創業歷程30

尚未有邦友留言

立即登入留言