日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

androidlistview的用法 androidinflate詳解( 二 )


public class myBean {private String text;//用來放文字的private int ImageID;//用來放圖片的public myBean(String text,int imageID){this.ImageID = imageID;this.text = text;}public String getText() {return text;}public void setText(String text) {this.text = text;}public int getImageID() {return ImageID;}public void setImageID(int imageID) {ImageID = imageID;}}然后我們就可以通過初始化不斷的New一個一個的數(shù)據(jù)了 , 但是我們怎么放進ListView里面呢?因為我們剛才用的是系統(tǒng)的ArrayAdapter來適配到ListView的 , 我們甚至連要適配的XML的界面都沒 。那我們先去做個我們要適配的界面去看看 , 于是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/ll_view"android:gravity="center"android:layout_margin="10dp"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:background="@mipmap/ic_launcher"android:id="@+id/headimage"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_marginLeft="20dp"android:layout_weight="1"android:text="你是SB"android:id="@+id/headtext"android:layout_width="0dp"android:layout_height="wrap_content" /><RadioGroupandroid:id="@+id/radioBtn"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:text="打他"android:id="@+id/radio2"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:text="不打"android:id="@+id/radio1"android:layout_width="wrap_content" android:layout_height="wrap_content" /></RadioGroup></LinearLayout></LinearLayout>于是我們把之前的R.layout.simple_list_item_1這XML換成我們直接做的 , 運行程序你就會發(fā)現(xiàn)程序崩了 。哈哈 , 不要緊這是正常的因為我們傳入的數(shù)據(jù)都沒用適配到我們的界面上 。所以我們就只能自己寫過一個適配器來適配我們自己的數(shù)據(jù) 。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);適配器代碼如下:
public class myAdapter extends ArrayAdapter {private final int ImageId;private String radiotext;public myAdapter(Context context, int headImage, List<myBean> obj){super(context,headImage,obj);ImageId = headImage;//這個是傳入我們自己定義的界面}@NonNull@Overridepublic View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {myBean myBean = (myBean) getItem(position);View view = LayoutInflater.from(getContext()).inflate(ImageId,parent,null);//這個是實例化一個我們自己寫的界面ItemLinearLayout linearLayout = view.findViewById(R.id.ll_view);ImageView headImage = view.findViewById(R.id.headimage);TextView headText = view.findViewById(R.id.headtext);RadioGroup radio = view.findViewById(R.id.radioBtn);headImage.setImageResource(myBean.getImageID());headText.setText(myBean.getText());radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {//檢查Radio Button那個被點擊了@Overridepublic void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {switch (i){case R.id.radio1:radiotext = "不打";break;case R.id.radio2:radiotext = "打他";break;}}});linearLayout.setOnClickListener(new View.OnClickListener() {//檢查哪一項被點擊了@Overridepublic void onClick(View view) {Toast.makeText(getContext(),"你點擊了第"+position+"項"+"你選擇"+radiotext,Toast.LENGTH_SHORT).show();}});return view; }}現(xiàn)在適配器也寫好了 , 你看定制ListView的2個步驟是不是就這樣就被我們解決了 , 然后我們就差適配了 。接下來我們來做一下適配:

推薦閱讀