ListView, and ArrayAdapter issue, How do I proceed?

痞子三分冷 提交于 2019-12-13 04:07:16

问题


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 ?


回答1:


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)));



回答2:


in getView(...) method you have to check price and set color of row...

see this customized listview..

http://samir-mangroliya.blogspot.in/p/android-customized-listview.html

i set row color as per odd and even row and

you can set checking price...

if(price < 1000){
       row.setBackgroundColor(Color.RED);
}else{
       row.setBackgroundColor(Color.Yellow);
}


来源:https://stackoverflow.com/questions/10617093/listview-and-arrayadapter-issue-how-do-i-proceed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!