这是基础班的东西吧,给你看看我们现在做的小案例
import java.util.ArrayList;
import com.itheima.news_listview.adapter.NewsAdapter;
import com.itheima.news_listview.bean.NewsBean;
import com.itheima.news_listview.utils.NewsUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
//1.找到控件
ListView lv_news = (ListView) findViewById(R.id.lv_news);
//2.获取新闻数据用list封装
ArrayList<NewsBean> allNews = NewsUtils.getAllNews(mContext);
//3.创建一个adapter设置给listview
NewsAdapter newsAdapter = new NewsAdapter(mContext, allNews);
lv_news.setAdapter(newsAdapter);
//4.设置listview条目的点击事件
lv_news.setOnItemClickListener(this);
}
//listview的条目点击时会调用该方法 parent:代表listviw view:点击的条目上的那个view对象 position:条目的位置 id: 条目的id
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//需要获取条目上bean对象中url做跳转
NewsBean bean = (NewsBean) parent.getItemAtPosition(position);
String url = bean.news_url;
//跳转浏览器
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
public class NewsAdapter extends BaseAdapter {
private ArrayList<NewsBean> list;
private Context context;
//通过构造方法接受要显示的新闻数据集合
public NewsAdapter(Context context,ArrayList<NewsBean> list){
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
//1.复用converView优化listview,创建一个view作为getview的返回值用来显示一个条目
if(convertView != null){
view = convertView;
}else {
//context:上下文, resource:要转换成view对象的layout的id, root:将layout用root(ViewGroup)包一层作为codify的返回值,一般传null
// view = View.inflate(context, R.layout.item_news_layout, null);//将一个布局文件转换成一个view对象
//通过LayoutInflater将布局转换成view对象
// view = LayoutInflater.from(context).inflate(R.layout.item_news_layout, null);
//通过context获取系统服务得到一个LayoutInflater,通过LayoutInflater将一个布局转换为view对象
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.item_news_layout, null);
}
//2.获取view上的子控件对象
ImageView item_img_icon = (ImageView) view.findViewById(R.id.item_img_icon);
TextView item_tv_des = (TextView) view.findViewById(R.id.item_tv_des);
TextView item_tv_title = (TextView) view.findViewById(R.id.item_tv_title);
//3.获取postion位置条目对应的list集合中的新闻数据,Bean对象
NewsBean newsBean = list.get(position);
//4.将数据设置给这些子控件做显示
item_img_icon.setImageDrawable(newsBean.icon);//设置imageView的图片
item_tv_title.setText(newsBean.title);
item_tv_des.setText(newsBean.des);
return view;
}
}
public class NewsUtils {
//封装新闻的假数据到list中返回
public static ArrayList<NewsBean> getAllNews(Context context) {
ArrayList<NewsBean> arrayList = new ArrayList<NewsBean>();
for(int i = 0 ;i <100;i++)
{
NewsBean newsBean = new NewsBean();
newsBean.title ="谢霆锋经纪人:偷拍系侵权行为:";
newsBean.des= "称谢霆锋隐私权收到侵犯,将保留追究法律责任";
newsBean.news_url= "http://www.sina.cn";
newsBean.icon = context.getResources().getDrawable(R.drawable.ic_launcher);//通过context对象将一个资源id转换成一个Drawable对象。
arrayList.add(newsBean);
NewsBean newsBean1 = new NewsBean();
newsBean1.title ="知情人:王菲是谢霆锋心头最爱的人";
newsBean1.des= "身边的人都知道谢霆锋最爱王菲,二人早有复合迹象";
newsBean1.news_url= "http://www.baidu.cn";
newsBean1.icon = context.getResources().getDrawable(R.drawable.icon);//通过context对象将一个资源id转换成一个Drawable对象。
arrayList.add(newsBean1);
NewsBean newsBean2 = new NewsBean();
newsBean2.title ="热烈祝贺黑马74高薪就业";
newsBean2.des= "74期平均薪资20000,其中有一个哥们超过10万,这些It精英都迎娶了白富美.";
newsBean2.news_url= "http://www.itheima.com";
newsBean2.icon = context.getResources().getDrawable(R.drawable.icon2);//通过context对象将一个资源id转换成一个Drawable对象。
arrayList.add(newsBean2);
}
return arrayList;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:orientation="horizontal" >
<ImageView
android:layout_gravity="center"
android:id="@+id/item_img_icon"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_marginRight="10dp"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:singleLine="true"
android:id="@+id/item_tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:text="title"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:maxLines="2"
android:id="@+id/item_tv_des"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="des"
android:textColor="#666666"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lv_news"
/>
</RelativeLayout>
这只是一个小小的案例,兄弟好好学吧,就业班进度非常快 |