listview的基本使用

来源:互联网 发布:java 2进制实战 编辑:程序博客网 时间:2024/05/22 11:42

  一、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和TextViewImageView 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



原创粉丝点击