如何在单击该列表视图中的行时从列表视图中获取特定列值

时间:2015-11-03 08:47:18

标签: android sqlite android-studio

//code to dislplay item in list view
            myList= controller.searchprop(prop_no.getText().toString());
            if (myList.size() != 0) {
                ListView lv = getListView();
                ListAdapter adapter = new SimpleAdapter(Search_Equipments.this, myList,
                        R.layout.searchview, new String[]{"old_prop", "new_prop", "serial_no", "item_name","responsibility_center"}, new int[]{
                        R.id.txtsearch_old_prop, R.id.txtsearch_new_prop, R.id.txtsearch_serial_no, R.id.txtsearch_item_name, R.id.txtsearch_center});
                setListAdapter(adapter);

                InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }else {
                Toast.makeText(Search_Equipments.this,"No Records Available, Please enter valid value", Toast.LENGTH_LONG).show();
            }

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

            HashMap<String,String> map =(HashMap<String,String>)lv.getItemAtPosition(position);
            //Cursor c = (Cursor) parent.getItemAtPosition(position);
            String prop = map.put(DBController.prop_no1);

            //String prop = String.valueOf(parent.getItemAtPosition(position - 1));
            Toast.makeText(Search_Equipments.this, prop, Toast.LENGTH_LONG).show();
        }
    });

有人可以帮助我点击后如何从listview中获取特定数据。或调试我的代码。谢谢 :) 有人可以帮助我点击后如何从列表视图中获取特定数据。或调试我的代码。谢谢:))

1 个答案:

答案 0 :(得分:0)

可能有更优雅的解决方案,但我最近遇到了这个问题(我是Android开发的新手),并想出了一个解决方案。

onItemClick()侦听器中,放置以下代码以访问视图中的数据:

//invoke a HashMap of the item at the given position using Parent.
HashMap<String, String> info = (HashMap<String, String>) parent.getItemAtPosition(position);

//Then access individual data items within the hash map using the key
String personName = info.get("first_name");

//Or change data within the hash map
info.put("first_name", "new first name");

我希望这能回答你的问题(如果我理解正确的话)。