How to get data from custom adapter in ListView?

24,347

Solution 1

inside onItemClick you can use the AdapterView argument,

NextRdv item = (NextRdv) parent.getItemAtPosition(position)

Solution 2

You can use below code

 lvNextRdv.setOnItemClickListener(new OnItemClickListener() {

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

       // selected item
       String selected =((TextView)view.findViewById(R.id.your_textView_item_id)).getText().toString();

        Toast toast=Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
        toast.show();

      }

});

using this u can get the data of any TextView inside custom adapter for selected row.

Hope will help you...

Share:
24,347
Kevin Gilles
Author by

Kevin Gilles

Updated on June 05, 2020

Comments

  • Kevin Gilles
    Kevin Gilles almost 4 years

    here is my problem :

    I created a custom adapter for my ListView and I get information out of a List that I give to that adapter in order to let him fulfill my ListView. He does everything correctly. Now I'd like to implement an OnItemClickListener and to get the data from it. But in that list I have 4 TextViews ...

    How can I retrieve the data ?

    I already know how to get the id and position since I display them in a Toast.

    Thanks a lot for your help !

    Here is the custom Adapter :

    public class NextRdvAdapter extends ArrayAdapter<NextRdv> {
    private List<NextRdv> listNextRdv = new ArrayList<NextRdv>(); 
    private Context context; 
    
    public NextRdvAdapter(List<NextRdv> nextRdvList, Context ctx) {
        super(ctx, R.layout.list_next_rdv, nextRdvList);
        this.listNextRdv = nextRdvList; 
        this.context = ctx;
    } 
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        if (convertView == null) { // This a new view we inflate the new layout 
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            convertView = inflater.inflate(R.layout.list_next_rdv, parent, false); 
            } // Now we can fill the layout with the right values 
    
        TextView tvTimeStart = (TextView) convertView.findViewById(R.id.next_time_start); 
        TextView tvTimeEnd = (TextView) convertView.findViewById(R.id.next_time_end); 
        TextView tvEnterprise = (TextView) convertView.findViewById(R.id.next_enterprise_name); 
        TextView tvAddress = (TextView) convertView.findViewById(R.id.next_address); 
        NextRdv rdv = listNextRdv.get(position); 
    
        tvTimeStart.setText(rdv.getStartTime());
        tvTimeEnd.setText(rdv.getEndTime());
        tvEnterprise.setText(rdv.getEnterprise());
        tvAddress.setText(rdv.getAddress());
    
    
        return convertView; 
        }
    }
    

    Here is my Fragment that posess the ListView :

    public class HomeFragment extends Fragment implements OnItemClickListener{
    
    float density;
    
    //Everything you need about this rdv
    private ListView lvThisRdv;
    private List<NextRdv> listThisRdv = new ArrayList<NextRdv>();
    private NextRdvAdapter ThisRdvAdapter;  
    private NextRdvListBouchon thisBouchon;
    
    //Everything you need about next rdv
    private ListView lvNextRdv;
    private List<NextRdv> listNextRdv = new ArrayList<NextRdv>();
    private NextRdvAdapter nextRdvAdapter;  
    private NextRdvListBouchon bouchon;
    
    public HomeFragment(){}
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    
    
        lvThisRdv = (ListView)rootView.findViewById(R.id.listView_this_RDV);
        thisBouchon = new NextRdvListBouchon();
        listThisRdv = thisBouchon.getBouchonInc();
        ThisRdvAdapter = new NextRdvAdapter(listThisRdv, getActivity());
        lvThisRdv.setAdapter(ThisRdvAdapter);
    
        //Displaying the list of the next meetings
        lvNextRdv = (ListView)rootView.findViewById(R.id.listView_next_RDV);
    
        //Get test data
        bouchon = new NextRdvListBouchon();
        listNextRdv = bouchon.getBouchon();
    
        nextRdvAdapter = new NextRdvAdapter(listNextRdv, getActivity());
        lvNextRdv.setAdapter(nextRdvAdapter);
    
        lvNextRdv.setOnItemClickListener(new OnItemClickListener() {
    
              @Override
              public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    
                  //
                  Toast toast = Toast.makeText(getActivity(), "Pos : " +position+ " / id : " + id, Toast.LENGTH_SHORT);
                  toast.show();
              }
    
        });
    
        return rootView;
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
    }
    }
    
  • Kevin Gilles
    Kevin Gilles almost 10 years
    Okay I'll try that, thanks for that amazing answer's speed !!
  • Raghunandan
    Raghunandan almost 10 years
    blackbelts answer is better. you have to initialize the view again
  • Kevin Gilles
    Kevin Gilles almost 10 years
    Worked like a charm, thanks again for both your pace and precision !
  • Neha Shukla
    Neha Shukla almost 10 years
    Yes that is also another way to do this
  • Kevin Gilles
    Kevin Gilles almost 10 years
    Thanks for your answer but Blackbelts already provided me with one that seems more convenient to me. Thanks for the help though !
  • Kevin Gilles
    Kevin Gilles almost 10 years
    So this means that the adapter will not only fetch the data but also keep the items in the list ? Do you have any link or any topic that would explain this point that I could read ?
  • Blackbelt
    Blackbelt almost 10 years
    Sorry can you rephrase?
  • Kevin Gilles
    Kevin Gilles almost 10 years
    When I look at my ListView I can see 4 TextViews but it's possible that I retrieve the object that I used to populate this LV before. So I suppose that the associated object is still here, linked somewhere to the position of my LV, it's not only read and destroyed, it's now part of the LV, do you know how to call this fact or do you have a link I could read for detailed explainations of what happen when I LV.setAdapter(myAdapter); ?
  • Blackbelt
    Blackbelt almost 10 years
    Dataset is strictly bounded to the adapter which in turn is strictly bounded with the ListView. Nothing is destroyed. Views are recycled but they are unrelated to the dataset
  • Kevin Gilles
    Kevin Gilles almost 10 years
    Thanks for that vocab, I'm going to study that point further. Wish you a great day !