将ListAdapter值设置为String

时间:2014-07-05 19:46:56

标签: android listview listadapter

我有以下创建ListAdapter的函数;

ListAdapter adapter = new SimpleAdapter(
                        AllProductsActivity.this, productsList,
                        R.layout.list_item, new String[] { TAG_PID, TAG_NAME, TAG_PHONE, TAG_ADDRESS, TAG_SIZE,  TAG_CHES, TAG_PE, TAG_PRICE},
                        new int[] { R.id.pid, R.id.name, R.id.phone, R.id.address, R.id.Small, R.id.XC, R.id.Pep, R.id.price});
                // updating listview
                setListAdapter(adapter);

更新列表视图后,我想获得具有最高pid值的第一个listview行,并将其值分配给某些String值。为了让你知道我想要什么,伪代码如下;

  String nam = highest pid.name;
  String pho = highest pid.phone;
  ...

关于如何实现这一目标的任何想法?

1 个答案:

答案 0 :(得分:0)

我认为您必须从listView中取回适配器检查该适配器中的所有项,以找出哪个具有最高的pid。

看起来像这样:

    SimpleAdapter mAdapter = (SimpleAdapter)listView.getAdapter();
    Map<String, String> maxIdMap = null;
    for(int i=0; i<mAdapter.getCount(); i++){
        Map<String, String> map = (Map<String, String>)mAdapter.getItem(i);
        Integer pid = Integer.parseInt(map.get(TAG_PID));
        if(maxIdMap==null){
            maxIdMap = map;
        } else if(pid>Integer.parseInt(maxIdMap.get(TAG_PID))){
            maxIdMap = map;
        }
    }
    if(maxIdMap!=null) {
        // There is at least one record
        String name = maxIdMap.get(TAG_NAME);
        String phone = maxIdMap.get(TAG_PHONE);
        Log.d("Test", "Name: " + name + " Phone: " + phone);
    }