listview的基本使用

  一、listview是一个非常复杂的控件, 这里先说说listview 数据的加载,已经listview点击item事件。 怎样删除整行。

  二、a、要想在listview里面加多个控件,我们需要一个视图, 视图里面包括要在listview里面呈现的控件

          b、有了视图以后,就要加载数据,这是使用了适配器。   我个人喜欢自定义适配器

          c、listview 点击item事件,      listview.setOnItemClickListener(new OnItemClickListener() {
                                                                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,   long arg3) {

                                                                       //处理代码

                                                                   }}

  三、注意: 在listview里面会出现,点击item无效。   这里有很多方法解决。 其一:在listview需要解析的视图里面 加上    android:descendantFocusability=                         "blocksDescendants"


看图:

a、加载视图    xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:descendantFocusability= "blocksDescendants"
    >

    <ImageView
        android:id="@+id/ivicon"
        android:layout_width="40dip"
        android:layout_height="wrap_content"
        />
<LinearLayout 

          android:layout_width="220dip"
        android:layout_height="90dip"
        android:orientation="vertical"
 
    >
    <TextView 
        android:id="@+id/tv1"
          android:layout_width="220dip"
        android:layout_height="40dip"
        />
    <TextView 
        android:id="@+id/tv2"
          android:layout_width="220dip"
        android:layout_height="50dip"
        />
    
    
</LinearLayout>
<Button 
    android:id="@+id/btndel"
          android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
       
    />

</LinearLayout>

java核心代码如下:
	LayoutInflater inflater = getLayoutInflater();
			View row = inflater.inflate(R.layout.rowlayout, null);
			// 从创建的行视图当中获取要填充数据的两个控件,ImageView和TextView
			ImageView iv = (ImageView) row.findViewById(R.id.ivicon);
			tv1 = (TextView) row.findViewById(R.id.tv1);
			tv2 = (TextView) row.findViewById(R.id.tv2);

listview点击事件:
list1.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				Log.e("-----------onItemClick", "positoin" + arg2 + ":arg1:"
						+ arg1);

				tv1 = (TextView) arg1.findViewById(R.id.tv1);
				Log.e("-----------", tv1.getText().toString());

				Toast.makeText(Zt_03_listview2Activity.this,
						tv1.getText().toString(), Toast.LENGTH_SHORT).show();
			}

		});
最后: 有写得不对的地方请大家指正, 一起探讨。


listview的基本使用完整代码: http://115.com/file/be0ukjn0#zt_03_listview2.rar



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值