您好,登录后才能下订单哦!
package com.example.ex_000_example;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
//准备好的图片资源
private int[] p_w_picpathList = new int[] { R.drawable.qq0, R.drawable.qq1,
R.drawable.qq2, R.drawable.qq3, R.drawable.qq4, R.drawable.qq5,
R.drawable.qq6, R.drawable.qq7, R.drawable.qq8, R.drawable.qq9,
R.drawable.qq10, R.drawable.qq11, R.drawable.qq12, R.drawable.qq13,
R.drawable.qq14 };
private ArrayList<MyDate> dateList = new ArrayList<MyDate>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 准备数据
for (int i = 0; i < p_w_picpathList.length; i++) {
MyDate myDate = new MyDate("数据来了"+i, p_w_picpathList[i]);
dateList.add(myDate);
}
ListView listView = (ListView) findViewById(R.id.listView1);
MyAdapter myAdapter = new MyAdapter();
// 设置适配器
listView.setAdapter(myAdapter);
}
//减少控件查找次数
class ViewHolder {
TextView textView;
ImageView p_w_picpathView;
}
class MyAdapter extends BaseAdapter {
// 设定每行的样式以及显示
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View inflate = null;
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
// 得到布局转换器
LayoutInflater inflater = getLayoutInflater();
// 将布局xml文件转换成布局对象
inflate = inflater.inflate(R.layout.list_item, null);
// 查找控件,一定要查找该布局对象上有的控件
holder.textView = (TextView) inflate
.findViewById(R.id.textView1);
holder.p_w_picpathView = (ImageView) inflate
.findViewById(R.id.p_w_picpathView1);
// 将保存控件的对象存入行布局对象的tag中
inflate.setTag(holder);
} else {
// 让那个已经完全消失的布局对象,去替代即将出现的
inflate = convertView;
// 从行布局对象中取出之前存好的控件
holder = (ViewHolder) inflate.getTag();
}
// 根据position从容器中取出数据
MyDate myDate = dateList.get(position);
// 将数据显示到控件上
holder.textView.setText(myDate.title);
holder.p_w_picpathView.setImageResource(myDate.p_w_picpath);
return inflate;
}
// 决定了listview显示的行数
@Override
public int getCount() {
// TODO Auto-generated method stub
return dateList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class MyDate{
String title;
int p_w_picpath;
public MyDate(String title, int p_w_picpath) {
super();
this.title = title;
this.p_w_picpath = p_w_picpath;
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。