如何覆盖ListAdpter的行为? [英] How to override ListAdpter's behavior?

查看:131
本文介绍了如何覆盖ListAdpter的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Android一个简单的蓝牙应用程序,我有一个ListActivity的
显示所有我已经找到了设备。

I am working on a simple Bluetooth application for android and I have a ListActivity that displays all the device I've found.

所以我有这样的ListAdapter:

So I have a ListAdapter like this:

deviceNameListAdapter = new ArrayAdapter<BluetoothDevice>(this,android.R.layout.simple_list_item_1,devices);

和它被设置为被显示。

不过,ListActivity显示设备的名称的设备的地址来代替。我猜的ListActivity将调用该项目中的设备的的ToString()方法时,它显示设备的列表。我只是想反正有我可以改变这种行为?
该BluetoothDevice类类呼吁的方法 .getName()这是应该叫什么。

However, the ListActivity displays the device's address instead of the device's name. I am guessing the ListActivity will call the item in devices's .toString() method when it displays the list of devices. I am just thinking is there anyway I can change this behaviour? The BluetoothDevice class have a method called .getName() which is what should be called.

干杯!

推荐答案

适配器类为例:

public class AdapterClass extends BaseAdapter {


     private List _list;

     public AdapterDirectories(List list){
          // Constructor with good parameters
          _list = list;
     }

    @Override
    public int getCount() {

        return _list.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        CellHolder cellHolder = null;
        // get
        if (convertView == null) {
            view = _inflater.inflate(R.layout.list_item, null);
            cellHolder = new CellHolder();
            cellHolder.textTitle = (TextView) view.findViewById(R.id.text_view_title);
            view.setTag(cellHolder);
        }
        else {
            cellHolder = (CellHolder) convertView.getTag();
        }
        // set
        cellHolder.textTitle.setText("");
        return view;
    }

    public class CellHolder {
        public TextView     textTitle;
    }
}

适配器设置列表视图:

setting the adapter to the list view :

_listView.setAdapter(_adapter);

这篇关于如何覆盖ListAdpter的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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