检查自定义列表视图中的值(如果已存在)

时间:2013-12-11 03:42:51

标签: java android listview android-listview hashmap

我在同一个活动中有两个自定义列表视图。然后,第二个listview可以从第一个listview中选择itemValue和itemID。那么现在,如何检查itemID是否已存在于第二个列表视图中?提前谢谢。

这是我的第二个listview

private void listOrder(String itemValue, String itemID)
{   
    HashMap<String, String> map = new HashMap<String, String>();


    String quantity = "1";

    map.put(FOODID3, itemID);
    map.put(FOODNAME3, itemValue);  
    map.put(FOODQUANTITY3, quantity);

    LIST3.add(map);

    LISTORDER = (ListView) findViewById(R.id.listOrder);

    List3Adapter adapter = new List3Adapter(MainActivity.this, LIST3);
    LISTORDER.setAdapter(adapter);

    adapter.notifyDataSetChanged();

    LISTORDER.setOnItemClickListener(new OnItemClickListener() 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {

        }
    });
}

这是listview的类

public class List3Adapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;

public List3Adapter(Activity a, ArrayList<HashMap<String, String>> d)
{
    activity = a;
    data = d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return data.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 vi = convertView;
    if (convertView == null)
    {
        vi = inflater.inflate(R.layout.row, null);
    }

    TextView foodID3 = (TextView)vi.findViewById(R.id.foodID3);
    TextView foodname3 = (TextView)vi.findViewById(R.id.foodName3);
    TextView foodquantity3 = (TextView)vi.findViewById(R.id.foodQuantity3);

    HashMap<String,String> food = new HashMap<String, String>();
    food = data.get(position);

    foodID3.setText(food.get(MainActivity.FOODID3));
    foodname3.setText(food.get(MainActivity.FOODNAME3));
    foodquantity3.setText(food.get(MainActivity.FOODQUANTITY3));

    return vi;
}
}

1 个答案:

答案 0 :(得分:2)

您可以检查哈希映射中的密钥以检查itemID是否已存在

if(data.containsKey(itemID))
{
  // your code
}

其中dataHashMap<String, String>对象