打开后一片空白,检查好多遍
先是Bean中的
package com.tong; public class Bean { private String view_title; private String view_content; private String view_time; private String view_phone; public Bean() { } public Bean(String view_title, String view_content, String view_time, String view_phone) { super(); this.view_title = view_title; this.view_content = view_content; this.view_time = view_time; this.view_phone = view_phone; } public String getView_title() { return view_title; } public void setView_title(String view_title) { this.view_title = view_title; } public String getView_content() { return view_content; } public void setView_content(String view_content) { this.view_content = view_content; } public String getView_time() { return view_time; } public void setView_time(String view_time) { this.view_time = view_time; } public String getView_phone() { return view_phone; } public void setView_phone(String view_phone) { this.view_phone = view_phone; } }
2.MainActivity中的’
package com.example.baseadapter; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.widget.ListView; import com.tong.Bean; public class MainActivity extends Activity { private ListView mlists; private List<Bean>mdatas; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mlists=(ListView)findViewById(R.id.listview); mlists.setAdapter(myAdapter); initData(); } private void initData() { //在该方法中初始化定义在Bean中的数据,先初始化List<Bean>中的mDatas mdatas=new ArrayList<Bean>(); Bean bean=new Bean("Android的使用技巧1","Android打造万能适配器来打造ListView和GridView", "2016-08-27","10086"); mdatas.add(bean); //用这两行代码加载数据,后面的bean不用重复定义 bean=new Bean("Android的使用技巧2","Android打造万能适配器来打造ListView和GridView", "2016-08-27","10086"); mdatas.add(bean); bean=new Bean("Android的使用技巧3","Android打造万能适配器来打造ListView和GridView", "2016-08-27","10086"); mdatas.add(bean); bean=new Bean("Android的使用技巧4","Android打造万能适配器来打造ListView和GridView", "2016-08-27","10086"); mdatas.add(bean); //初始化这些数据之后就需要新建一个适配器MyAdapter //数据传入后,加载适配器,需要传入两个参数,第一个Activity,第二个是mdatas myAdapter=new MyAdapter(this,mdatas);//然后在listview中加载该适配器 } }
3.MyAdapter中的
package com.example.baseadapter; import java.util.List; 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 com.tong.Bean; public class MyAdapter extends BaseAdapter{ private LayoutInflater inflater; private List<Bean> mdatas; //声明这个构造方法Context用于加载我们的布局文件 public MyAdapter(Context context, List<Bean>datas){ inflater=LayoutInflater.from(context); mdatas=datas;//获取到的数据集用于编写下面的属性 } @Override public int getCount() { return mdatas.size(); } @Override public Object getItem(int i) { return mdatas.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder=null;//声明一个ViewHolder if(view==null){ //判断是否已经加载,加载定义的布局 view=inflater.inflate(R.layout.listview_data,viewGroup,false); viewHolder=new ViewHolder(); viewHolder.mtitle= (TextView) view.findViewById(R.id.textview1); viewHolder.mcontent= (TextView) view.findViewById(R.id.textview2); viewHolder.mtime= (TextView) view.findViewById(R.id.textview3); viewHolder.mphone= (TextView) view.findViewById(R.id.textview4); view.setTag(viewHolder); }else { viewHolder= (ViewHolder) view.getTag(); } Bean bean=mdatas.get(i);//定义Bean用来获取当前显示的数据 viewHolder.mtitle.setText(bean.getView_title()); viewHolder.mcontent.setText(bean.getView_content()); viewHolder.mtime.setText(bean.getView_time()); viewHolder.mphone.setText(bean.getView_phone()); return view; //这步完成之后去MainActivity.java文件中加载MyAdapter,先新建一个MyAdapter型的变量 } //定义一个ViewHolder类用于存放数据类型及性能优化 private class ViewHolder{ TextView mtitle; TextView mcontent; TextView mtime; TextView mphone; } }