ListView控件和ArrayAdapter的问题,我该如何继续? [英] ListView, and ArrayAdapter issue, How do I proceed?

查看:154
本文介绍了ListView控件和ArrayAdapter的问题,我该如何继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品类别,其中有三个领域: -


  1. 标识

  2. 名称

  3. 价格

在我的code我创建了一个列表与LT;产品与GT; productList的= Collections.synchronizedList(新的ArrayList<产品及GT;());

本产品列表来创建一个 ArrayAdapter<产品与GT;适配器= ArrayAdapter<产品及GT;(),我填一个单独的线程的productList的适配器和相应通知。这是工作的精绝。

现在,


  1. 我想改变某些特定产品的颜色(比如价格< 1000)。

  2. ListView中的每一行应该包含4个元素的产品形象,名称,说明和价格。

  3. 当用户点击该产品,在上下文菜单选项,即买产品,查看产品应显示。

我看过相关的一些博客和线程。不过我不能决定从哪里开始,我读到一个ArrayAdapter的定制,覆盖getView(),自定义列表过滤器等哪种方式将是最适合我的需求......换句话说,如何能自定义适配器和列表过滤器的好处我 ?


解决方案

您应该扩展的 BaseAdapter ,并为每个项目提供自己的布局(getView())。不要忘了管理视图循环,也许使用ViewHolder范例。

修改

我没有使用很多ListAdpater,因为它绑定到只有一个ListView。有时候,我需要一个GridView适配器,以及BaseAdapter给了我足够的自由,对所有用例。

BaseAdapter的例子​​:

 公共类FanAdapter延伸BaseAdapter {    私人列表<风扇> mFans;
    私人活动mContext;    公共FanAdapter(Activity上下文,列表<风扇>风扇){
        mContext =背景;
        mFans =风扇;
    }    私有类ViewHolder {        公众形象ImageView的;
        公众的TextView的firstName;
        公众的TextView lastName的;
    }    @覆盖
    公共查看getView(INT位置,查看视图的ViewGroup容器){
        如果(查看== NULL){
            鉴于= LayoutInflater.from(mContext).inflate(R.layout.fan_item,集装箱,FALSE);
        }        ViewHolder viewHolder =(ViewHolder)view.getTag();
        如果(viewHolder == NULL){
            viewHolder =新ViewHolder();
            viewHolder.image =(ImageView的)view.findViewById(R.id.image);
            viewHolder.firstName =(TextView中)view.findViewById(R.id.firstname);
            viewHolder.lastName =(TextView中)view.findViewById(R.id.lastname);
            view.setTag(viewHolder);
        }        //这里设置从我的风扇对象值到我的项目领域
        viewHolder.firstName.setText(fan.getFirstName());
        (......)
        返回视图。
    }    @覆盖
    公众诠释的getCount(){
        如果(mFans!= NULL){
            返回mFans.size();
        }其他{
            返回0;
        }
    }    @覆盖
    公共对象的getItem(INT位置){
        返回mFans.get(位置);
    }    @覆盖
    众长getItemId(INT位置){
        返回的位置;
    }
}

可以包含一个ListView或ListActivity(一种在其布局具有特殊ID一个ListView)的活动使用

 < ListView控件
        机器人:ID =@ ID /安卓名单
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:cacheColorHint =@机器人:彩色/透明/>

这样,你的ListActivity将膨胀的观点就能做出findViewById()调用和getListView()将返回这个内部的ListView。这是一个小黑客,你可以把自己的ListView与另一个ID,使findViewById()自己。对于ListActivity,还有另一种破解:如果ListActivity发现再次与一个特殊的ID空视图,它会显示在列表是空的:

 <包括
    机器人:ID =@ + ID /空
    布局=@布局/空
    机器人:知名度=水涨船高
    机器人:layout_gravity =中心/>

然后在你的ListView,无论你使用的活动或ListActivity,您可以在ListView控件设置你的适配器:

  getListView()setAdapter(新FanAdapter(这一点,myFanDataArray)))。

I have a Product Class, Which has three fields:-

  1. id
  2. name
  3. price

In my code I create a List<Product> productList = Collections.synchronizedList(new ArrayList<Product>());

This product list is used to create a ArrayAdapter<Product> adapter = ArrayAdapter<Product>(), I fill the productList in a separate thread and adapter is notified accordingly. It is working absolutely fine.

Now,

  1. I want to change the color of the some specific products (say for price < 1000).
  2. Each row of ListView should contain 4 elements product image,name, desc and price.
  3. When User clicks the Product, in a context menu options i.e. buy Product, View Product should be displayed.

I have read few blogs and threads related to that. Still I cant decide where to begin, I read about the customization of the ArrayAdapter, overriding getView(), custom list filters etc. Which way will be the best for my requirement... in other words How can custom adapters and list filters benefit me ?

解决方案

You should extend BaseAdapter and provide your own layout for each item (getView()). Don't forget to manage the view recycling and maybe use the ViewHolder paradigm.

EDIT

I didn't use a lot the ListAdpater, because it binds to a ListView only. Sometimes I need an adapter for a GridView, and the BaseAdapter gives me enough freedom for all use cases.

Example of BaseAdapter:

public class FanAdapter extends BaseAdapter {

    private List<Fan> mFans;
    private Activity mContext;

    public FanAdapter(Activity context, List<Fan> fans) {
        mContext = context;
        mFans = fans;
    }

    private class ViewHolder {

        public ImageView image;
        public TextView firstName;
        public TextView lastName;   
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.fan_item, container, false);
        }

        ViewHolder viewHolder = (ViewHolder) view.getTag();
        if(viewHolder == null){
            viewHolder = new ViewHolder();
            viewHolder.image = (ImageView) view.findViewById(R.id.image);
            viewHolder.firstName = (TextView) view.findViewById(R.id.firstname);
            viewHolder.lastName = (TextView) view.findViewById(R.id.lastname);
            view.setTag(viewHolder);
        }

        // setting here values to the fields of my items from my fan object
        viewHolder.firstName.setText(fan.getFirstName());
        (...)


        return view;
    }

    @Override
    public int getCount() {
        if (mFans != null) {
            return mFans.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return mFans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}

You can use it with an Activity containing a ListView or a ListActivity (having in its layout a ListView with a special id):

<ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@android:color/transparent" />

This way, your ListActivity that will inflate the view will be able to make a findViewById() call and getListView() will return this internal listView. It's a small hack, you can put your own listView with another id and make the findViewById() yourself. For The ListActivity, there's another hack: if the ListActivity finds an empty view with again a special id, it will be shown when the list is empty:

<include
    android:id="@+id/empty"
    layout="@layout/empty"
    android:visibility="gone"
    android:layout_gravity="center" />

Then on your listView, whether you used an Activity or ListActivity, you can set your adapter on the ListView:

getListView().setAdapter(new FanAdapter(this, myFanDataArray)));

这篇关于ListView控件和ArrayAdapter的问题,我该如何继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆