您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。