How to get the value of a Listview item which is clicked in android?

前端 未结 4 749
情话喂你
情话喂你 2020-12-15 11:05

I have this below code access the ListView item value into string and display it in alert?

ListView shot = getListView();
shot.setOnItemClickListener(this);
         


        
相关标签:
4条回答
  • 2020-12-15 11:26

    Maybe you can try this

    String data = (String)shot.getItemAtPosition(arg2);
    AlertDialog.Builder adb = new AlertDialog.Builder(arg1.getContext());              
    adb.setMessage(data).show();
    
    0 讨论(0)
  • 2020-12-15 11:39

    To get the value of your model

    adaptor.getItem(position).getCardName();

    0 讨论(0)
  • 2020-12-15 11:43

    This gives you the exact value of the item clicked. Check the log

    ListView shot = getListView();
    shot.setOnItemClickListener(this);
    
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    
        String val =(String) parent.getItemAtPosition(position);
        System.out.println("Value is "+val); 
    }
    
    0 讨论(0)
  • 2020-12-15 11:44

    maybe this example will help you

      lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_SHORT).show();
        }
      });
    

    https://developer.android.com/reference/android/widget/ListView.html

    0 讨论(0)
提交回复
热议问题