2013-12-11 228 views
1

我在同一活動中有兩個自定義listview。然後,第二個listview可以獲得從第一個listview中選擇的itemValue和itemID。那麼現在,我該如何檢查itemID是否已經存在於第二個listview中?提前致謝。檢查自定義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; 
} 
} 
+0

你在哪裏調用listOrder方法? –

+0

@Tamanna onItemClick – user3040029

+0

第一個listview然後你會得到什麼?你可以檢查哈希映射中的鍵來檢查f條目ID是否已經存在 –

回答

2

您可以爲您在哈希表的關鍵,檢查ITEMID已經存在與否

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

其中dataHashMap<String, String>對象

+0

對不起,實際上我在執行此檢查MainActivity或我的課程時感到困惑。你有沒有關於containskey建議的例子?謝謝! – user3040029

+0

你可以根據你的需要來檢查 –