主要类
package com.example.shipeizia;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
List<itembean> itembeans = new ArrayList<>();
for (int i = 0; i < 20; i++) {
itembeans.add(new itembean(R.drawable.ic_launcher,"我是标题"+i, "我是内容,我是内容"+i));
}
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new shipeiqi(this,itembeans));
}
}
封装类
package com.example.shipeizia;
public class itembean {
public int tupianid;
public String biaotiString;
public String neirongString;
public itembean(int tupianid,String biaotiString,String neirongString){
this.neirongString= neirongString;
this.tupianid= tupianid;
this.biaotiString=biaotiString;
}
}
适配器
package com.example.shipeizia;
import java.util.List;
import java.util.zip.Inflater;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class shipeiqi extends BaseAdapter {
public List<itembean> mlist;
public LayoutInflater mInflater;
public shipeiqi(Context context, List<itembean> list){
mlist=list;//数据源和数据适配器关联
mInflater = LayoutInflater.from(context);//context要是用当前的适配器界面对象mInflater布局装载器对象
}
@Override
public int getCount() {
// 返回ListView 显示的数据量
return mlist.size();
}
@Override
public Object getItem(int arg0) {
// 制定索引和对应的数据项
return mlist.get(arg0);
}
@Override
public long getItemId(int arg0) {
// 返回制定索引对应的数据项
return arg0;
}
@Override
public View getView(int a, View b, ViewGroup c) {
ViewHolder viewHolder=null ;
if (b==null) {
viewHolder = new ViewHolder();
b=mInflater.inflate(R.layout.item, null);
viewHolder.imageView=(ImageView) b.findViewById(R.id.tupian);
viewHolder.biaoti=(TextView) b.findViewById(R.id.biaoti);
viewHolder.neirong=(TextView) b.findViewById(R.id.neirong);
b.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) b.getTag();
}
itembean bean = mlist.get(a);
viewHolder.imageView.setImageResource(bean.tupianid);
viewHolder.biaoti.setText(bean.biaotiString);
viewHolder.neirong.setText(bean.neirongString);
return b;
}
class ViewHolder{
public ImageView imageView;
public TextView biaoti;
public TextView neirong;
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦