总结下listview,最基础使用方记录:
public class MainActivity extends ActionBarActivity {
List
data=new ArrayList
();
myadapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
DataView();
}
private void initView() {
ListView list=(ListView)findViewById(R.id.lv_basics);
adapter=new myadapter(this);
list.setAdapter(adapter);
}
public class myadapter extends BaseAdapter{
LayoutInflater inflater;
public myadapter(Context context){
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return data.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
arg1=inflater.inflate(R.layout.listview_layout, null);
ImageView img_second_dto =(ImageView)arg1.findViewById(R.id.img_basics_dto);
TextView tv_second_dto=(TextView)arg1.findViewById(R.id.tv_basics_dto);
img_second_dto.setImageResource(data.get(arg0).getPic());
tv_second_dto.setText(data.get(arg0).getName());
return arg1;
}
}
private void DataView() {
data.clear();
ListDTO data1 =new ListDTO();
data1.setName("第一个");
data1.setPic(R.drawable.ic_launcher);
data.add(data1);
ListDTO data2 =new ListDTO();
data2.setName("第一个");
data2.setPic(R.drawable.ic_launcher);
data.add(data2);
ListDTO data3 =new ListDTO();
data3.setName("第一个");
data3.setPic(R.drawable.ic_launcher);
data.add(data3);
ListDTO data4 =new ListDTO();
data4.setName("第一个");
data4.setPic(R.drawable.ic_launcher);
data.add(data4);
ListDTO data5 =new ListDTO();
data5.setName("第一个");
data5.setPic(R.drawable.ic_launcher);
data.add(data5);
ListDTO data6 =new ListDTO();
data6.setName("第一个");
data6.setPic(R.drawable.ic_launcher);
data.add(data6);
ListDTO data7 =new ListDTO();
data7.setName("第一个");
data7.setPic(R.drawable.ic_launcher);
data.add(data7);
}
}