ListView控件 数据加载
这个太常用了 做下记录。
ListView可以简单的呈现大数据的浏览,加载图片,文字。。等等
网络也有很多Demo,我就不多说了
首先给个ListView控件:listview_widget.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listview_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- 列表控件 --> <ListView android:id="@+id/MainListView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
给每个Item添加”样式“ listview_item_layout.xml
<?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:orientation="horizontal" > <!-- 列表Item --> <ImageView android:id="@+id/list_image" android:layout_width="80dp" android:layout_height="80dp" android:contentDescription="@string/app_name" android:paddingLeft="10dp" android:paddingRight="10dp" android:tag="@string/goodsinfo_img_name" android:src="@drawable/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/list_text_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/goodsinfo_name" /> <TextView android:id="@+id/list_text_2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:text="@string/goodsinfo_price" /> </LinearLayout> <TextView android:id="@+id/list_text_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/goodsinfo_info" /> </LinearLayout> </LinearLayout>
布局界面就这样。
代码实现:
首先给listView一个适配器,加载数据用:
View Code
/** * @author admin * ListView 适配器 */ public class MyListAdapter extends BaseAdapter { private static String TAG = "MyListAdapter"; private Activity mActivity; private List<GoodsInfo> goodsList; private ViewHolder holder = null; private GoodsInfo _info = null; private View itemView = null; public MyListAdapter(Activity context, List<GoodsInfo> list) { // TODO Auto-generated constructor stub this.mActivity = context; this.goodsList = list; } @Override public int getCount() { // TODO Auto-generated method stub return goodsList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return goodsList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub holder = new ViewHolder(); LayoutInflater inflater = mActivity.getLayoutInflater(); itemView = inflater.inflate(R.layout.listview_item_layout, null); _info = goodsList.get(position); holder.goodsName = (TextView) itemView.findViewById(R.id.list_text_1); holder.goodsPrice = (TextView) itemView.findViewById(R.id.list_text_2); holder.goodsInfo = (TextView) itemView.findViewById(R.id.list_text_3); holder.goodsImg = (ImageView) itemView.findViewById(R.id.list_image); itemView.setTag(holder); holder.goodsName.setText(_info.getGoodsName()); String _price=Float.toString(_info.getGoodsPrice()); holder.goodsPrice.setText(_price); holder.goodsInfo .setText(_info.getGoodsInfos()); //imageView.setImageDrawable(); holder.goodsImg.setImageResource(_info.getGoodsImage()); return itemView; } public final class ViewHolder{ public ImageView goodsImg = null; public TextView goodsName = null; public TextView goodsInfo = null; public TextView goodsPrice = null; } }
然后找到View(这里用的是动态布局),设置适配器,添加Item点击事件结束。
View Code
(GoodsInfo 类就不给出)
此代码参考网络各个文章,自己做记录,如有雷同,说明看过你的牛文,再次感谢!