zoukankan      html  css  js  c++  java
  • android ListView

    1 得到选择item的值

            mContactList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cursor c = (Cursor) mContactList.getItemAtPosition(position);
    String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    }
    });
    //根据点击位置取得item,转换为Cursor
            mContactList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    cursor.moveToPosition(position);
    String name = cursor.getString(
    cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    }
    });

    //cursor为外部查询得到,在内存中保存一个cursor,根据点击位置去cursor取值

     2 自定义ListView

      1)首先需要自定义item的layout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation
    ="horizontal"
    android:layout_width
    ="match_parent"
    android:layout_height
    ="match_parent">

    <ImageView android:id="@+id/icon"
    android:layout_width
    ="48dip"
    android:layout_height
    ="48dip"/>

    <TextView android:id="@+id/text"
    android:layout_gravity
    ="center_vertical"
    android:layout_width
    ="0dip"
    android:layout_weight
    ="1.0"
    android:layout_height
    ="wrap_content"/>

    </LinearLayout>


      2) 自定义Adapter,并且在构造函数中传值

    package com.example.android.contactmanager;

    import android.content.Context;
    import android.database.Cursor;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    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 ContactItemAdapter extends BaseAdapter{

    private LayoutInflater mInflater;
    private Bitmap mIcon1;
    private Bitmap mIcon2;

    private Cursor cursor=null;

    public ContactItemAdapter(Context context,Cursor cursor) {
    this.cursor=cursor;
    // Cache the LayoutInflate to avoid asking for a new one each time.
    mInflater = LayoutInflater.from(context);

    // Icons bound to the rows.
    mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
    mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
    }

    /**
    * The number of items in the list is determined by the number of speeches
    * in our array.
    *
    *
    @see android.widget.ListAdapter#getCount()
    */
    public int getCount() {
    return cursor.getCount();
    }

    /**
    * Since the data comes from an array, just returning the index is
    * sufficent to get at the data. If we were using a more complex data
    * structure, we would return whatever object represents one row in the
    * list.
    *
    *
    @see android.widget.ListAdapter#getItem(int)
    */
    public Object getItem(int position) {
    return position;
    }

    /**
    * Use the array index as a unique id.
    *
    *
    @see android.widget.ListAdapter#getItemId(int)
    */
    public long getItemId(int position) {
    return position;
    }

    /**
    * Make a view to hold each row.
    *
    *
    @see android.widget.ListAdapter#getView(int, android.view.View,
    * android.view.ViewGroup)
    */
    public View getView(int position, View convertView, ViewGroup parent) {
    // A ViewHolder keeps references to children views to avoid unneccessary calls
    // to findViewById() on each row.
    ViewHolder holder;

    // When convertView is not null, we can reuse it directly, there is no need
    // to reinflate it. We only inflate a new View when the convertView supplied
    // by ListView is null.
    if (convertView == null) {
    convertView = mInflater.inflate(R.layout.test_list_item_icon_text, null);

    // Creates a ViewHolder and store references to the two children views
    // we want to bind data to.
    holder = new ViewHolder();
    holder.text = (TextView) convertView.findViewById(R.id.text);
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);

    convertView.setTag(holder);
    } else {
    // Get the ViewHolder back to get fast access to the TextView
    // and the ImageView.
    holder = (ViewHolder) convertView.getTag();
    }

    cursor.moveToPosition(position);

    // Bind the data efficiently with the holder.
    holder.text.setText(cursor.getString(1));
    holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

    return convertView;
    }

    static class ViewHolder {
    TextView text;
    ImageView icon;
    }

    }


      3)在Activity中调用

      

       public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Cursor cursor=getContacts();
    setListAdapter(new ContactItemAdapter(this,cursor));
    }

    资料:

           ListView使用初步

           ListView添加事件并获取选中项的值

  • 相关阅读:
    用java程序启动电脑中的软件 分类: java 20091105 01:03 355人阅读 评论(0) 收藏
    关于nextSibling的用法 20091103 01:53 311人阅读 评论(0) 收藏
    HTML中的标签用法 分类: 网页编程【html、js】 20091103 00:53 282人阅读 评论(0) 收藏
    我总结的正则表达式 分类: 正则表达式 20091106 23:01 298人阅读 评论(0) 收藏
    [正则表达式]非常经典的正则表达式 分类: 正则表达式 20091106 03:18 393人阅读 评论(0) 收藏
    读取zip,过滤中文名称 分类: java 20091106 22:46 385人阅读 评论(0) 收藏
    MS SQL与Oracle常用函数对比 分类: 数据库 20091105 23:41 259人阅读 评论(0) 收藏
    读取zip,不过滤中文名,即中文名称也能成功读取 分类: java 20091106 22:48 296人阅读 评论(0) 收藏
    根据字节数截取字符串... 分类: java 20091105 19:21 235人阅读 评论(0) 收藏
    Pattern类 分类: java 20091105 01:00 240人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/myparamita/p/2257443.html
Copyright © 2011-2022 走看看