androidinflate详解 androidlistview的用法( 二 )


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个办法是否就如许就被咱们处置了 , 而后咱们就差适配了 。接下来咱们来做一下适配:
public class MainActivity extends AppCompatActivity {private List<myBean> myBeanList = new ArrayList<>();//用来寄存数据的数组@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView listView = (ListView) findViewById(R.id.listview);init();myAdapter adapter = new myAdapter(MainActivity.this,R.layout.myitem,myBeanList);listView.setAdapter(adapter);}private void init(){//初始化数据myBean bean1 = new myBean("aa",R.mipmap.ic_launcher);myBeanList.add(bean1);myBean bean2 = new myBean("ss",R.mipmap.ic_launcher);myBeanList.add(bean2);myBean bean3 = new myBean("jj",R.mipmap.ic_launcher);myBeanList.add(bean3);myBean bean4 = new myBean("hh",R.mipmap.ic_launcher);myBeanList.add(bean4);myBean bean5 = new myBean("dd",R.mipmap.ic_launcher);myBeanList.add(bean5);myBean bean6 = new myBean("cc",R.mipmap.ic_launcher);myBeanList.add(bean6);myBean bean7 = new myBean("bb",R.mipmap.ic_launcher);myBeanList.add(bean7);myBean bean8 = new myBean("jj",R.mipmap.ic_launcher);myBeanList.add(bean8);myBean bean9 = new myBean("kk",R.mipmap.ic_launcher);myBeanList.add(bean9);}做到这边我想大师都简直发端控制了如何定制ListView了吧?哦对了!我在写适配重的功夫特地把监听事变写进去了 , 固然在主类写也是不妨的 , 然而不倡导如许做 。至于干什么?你本人试试就领会了 , 由于纸上得来终觉浅嘛 。
结果上一张功效图吧:
【androidinflate详解androidlistview的用法】创造有许多BUG呢!然而那都不是事 , 究竟没有哪个APP是没有BUG的是否?

推荐阅读