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一个一个的数据了 , 但是我们怎么放进ListView里面呢?因为我们刚才用的是系统的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换成我们直接做的 , 运行程序你就会发现程序崩了 。哈哈 , 不要紧这是正常的因为我们传入的数据都没用适配到我们的界面上 。所以我们就只能自己写过一个适配器来适配我们自己的数据 。
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; }}现在适配器也写好了 , 你看定制ListView的2个步骤是不是就这样就被我们解决了 , 然后我们就差适配了 。接下来我们来做一下适配:

推荐阅读