get a list item from a ListView











up vote
0
down vote

favorite












I'm interested in getting an object reference to a list item in a ListView when that item is clicked. Every item on my custom item XML contains a Button and a TextView that are hidden by default. The click on an item should change their visibility to visible.

Here's what I mean, done in the integrated graphic editor.



enter image description here



Should become this



enter image description here



This is my custom getView for the AdapterView





@Override
public View getView(int position, View v, ViewGroup viewGroup){

if(v==null){
LayoutInflater li= LayoutInflater.from(getContext());
v= li.inflate(R.layout.custom_list_item, null);
}

Student s= getItem(position);
TextView name= v.findViewById(R.id.txt_name);
TextView surname= v.findViewById(R.id.txt_surname);
TextView id= v.findViewById(R.id.txt_id);

//following methods come from my Student class
if(s != null){
name.setText(s.getName());
surname.setText(s.getSurname());
id.setText(s.getId());
}
return v;
}




and here's what I (would like to) do in my Activity, but doesn't work by now.





ListView list= findViewById(R.id.advanced_list);
final StudentAdapter adapter= new StudentAdapter(
this,
R.layout.custom_list_item,
getStudents()
);

list.setAdapter(adapter);


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

TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);

tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});









share|improve this question




















  • 1




    Show some code pls.
    – W. Seun
    Nov 11 at 13:18






  • 1




    Read the documentation for ListView.OnItemClickListener
    – Jim Rhodes
    Nov 11 at 13:26










  • You can check out my answer below on how to change the visibility when your button is pressed and also why it is that your code is not working. I've also given the link to a good tutorial in case you are still having difficulties with this. My answer has both a basic answer and elaborated answer, (the elaborated answer contains the code). Hope this helps!
    – Ishaan
    Nov 11 at 14:02

















up vote
0
down vote

favorite












I'm interested in getting an object reference to a list item in a ListView when that item is clicked. Every item on my custom item XML contains a Button and a TextView that are hidden by default. The click on an item should change their visibility to visible.

Here's what I mean, done in the integrated graphic editor.



enter image description here



Should become this



enter image description here



This is my custom getView for the AdapterView





@Override
public View getView(int position, View v, ViewGroup viewGroup){

if(v==null){
LayoutInflater li= LayoutInflater.from(getContext());
v= li.inflate(R.layout.custom_list_item, null);
}

Student s= getItem(position);
TextView name= v.findViewById(R.id.txt_name);
TextView surname= v.findViewById(R.id.txt_surname);
TextView id= v.findViewById(R.id.txt_id);

//following methods come from my Student class
if(s != null){
name.setText(s.getName());
surname.setText(s.getSurname());
id.setText(s.getId());
}
return v;
}




and here's what I (would like to) do in my Activity, but doesn't work by now.





ListView list= findViewById(R.id.advanced_list);
final StudentAdapter adapter= new StudentAdapter(
this,
R.layout.custom_list_item,
getStudents()
);

list.setAdapter(adapter);


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

TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);

tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});









share|improve this question




















  • 1




    Show some code pls.
    – W. Seun
    Nov 11 at 13:18






  • 1




    Read the documentation for ListView.OnItemClickListener
    – Jim Rhodes
    Nov 11 at 13:26










  • You can check out my answer below on how to change the visibility when your button is pressed and also why it is that your code is not working. I've also given the link to a good tutorial in case you are still having difficulties with this. My answer has both a basic answer and elaborated answer, (the elaborated answer contains the code). Hope this helps!
    – Ishaan
    Nov 11 at 14:02















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm interested in getting an object reference to a list item in a ListView when that item is clicked. Every item on my custom item XML contains a Button and a TextView that are hidden by default. The click on an item should change their visibility to visible.

Here's what I mean, done in the integrated graphic editor.



enter image description here



Should become this



enter image description here



This is my custom getView for the AdapterView





@Override
public View getView(int position, View v, ViewGroup viewGroup){

if(v==null){
LayoutInflater li= LayoutInflater.from(getContext());
v= li.inflate(R.layout.custom_list_item, null);
}

Student s= getItem(position);
TextView name= v.findViewById(R.id.txt_name);
TextView surname= v.findViewById(R.id.txt_surname);
TextView id= v.findViewById(R.id.txt_id);

//following methods come from my Student class
if(s != null){
name.setText(s.getName());
surname.setText(s.getSurname());
id.setText(s.getId());
}
return v;
}




and here's what I (would like to) do in my Activity, but doesn't work by now.





ListView list= findViewById(R.id.advanced_list);
final StudentAdapter adapter= new StudentAdapter(
this,
R.layout.custom_list_item,
getStudents()
);

list.setAdapter(adapter);


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

TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);

tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});









share|improve this question















I'm interested in getting an object reference to a list item in a ListView when that item is clicked. Every item on my custom item XML contains a Button and a TextView that are hidden by default. The click on an item should change their visibility to visible.

Here's what I mean, done in the integrated graphic editor.



enter image description here



Should become this



enter image description here



This is my custom getView for the AdapterView





@Override
public View getView(int position, View v, ViewGroup viewGroup){

if(v==null){
LayoutInflater li= LayoutInflater.from(getContext());
v= li.inflate(R.layout.custom_list_item, null);
}

Student s= getItem(position);
TextView name= v.findViewById(R.id.txt_name);
TextView surname= v.findViewById(R.id.txt_surname);
TextView id= v.findViewById(R.id.txt_id);

//following methods come from my Student class
if(s != null){
name.setText(s.getName());
surname.setText(s.getSurname());
id.setText(s.getId());
}
return v;
}




and here's what I (would like to) do in my Activity, but doesn't work by now.





ListView list= findViewById(R.id.advanced_list);
final StudentAdapter adapter= new StudentAdapter(
this,
R.layout.custom_list_item,
getStudents()
);

list.setAdapter(adapter);


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

TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);

tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});






android android-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:49

























asked Nov 11 at 13:16









davide m.

306




306








  • 1




    Show some code pls.
    – W. Seun
    Nov 11 at 13:18






  • 1




    Read the documentation for ListView.OnItemClickListener
    – Jim Rhodes
    Nov 11 at 13:26










  • You can check out my answer below on how to change the visibility when your button is pressed and also why it is that your code is not working. I've also given the link to a good tutorial in case you are still having difficulties with this. My answer has both a basic answer and elaborated answer, (the elaborated answer contains the code). Hope this helps!
    – Ishaan
    Nov 11 at 14:02
















  • 1




    Show some code pls.
    – W. Seun
    Nov 11 at 13:18






  • 1




    Read the documentation for ListView.OnItemClickListener
    – Jim Rhodes
    Nov 11 at 13:26










  • You can check out my answer below on how to change the visibility when your button is pressed and also why it is that your code is not working. I've also given the link to a good tutorial in case you are still having difficulties with this. My answer has both a basic answer and elaborated answer, (the elaborated answer contains the code). Hope this helps!
    – Ishaan
    Nov 11 at 14:02










1




1




Show some code pls.
– W. Seun
Nov 11 at 13:18




Show some code pls.
– W. Seun
Nov 11 at 13:18




1




1




Read the documentation for ListView.OnItemClickListener
– Jim Rhodes
Nov 11 at 13:26




Read the documentation for ListView.OnItemClickListener
– Jim Rhodes
Nov 11 at 13:26












You can check out my answer below on how to change the visibility when your button is pressed and also why it is that your code is not working. I've also given the link to a good tutorial in case you are still having difficulties with this. My answer has both a basic answer and elaborated answer, (the elaborated answer contains the code). Hope this helps!
– Ishaan
Nov 11 at 14:02






You can check out my answer below on how to change the visibility when your button is pressed and also why it is that your code is not working. I've also given the link to a good tutorial in case you are still having difficulties with this. My answer has both a basic answer and elaborated answer, (the elaborated answer contains the code). Hope this helps!
– Ishaan
Nov 11 at 14:02














2 Answers
2






active

oldest

votes

















up vote
1
down vote













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

TextView tel= findViewById(R.id.txt_tel);
Button delete= findViewById(R.id.btn_delete);

tel.setVisibility(View.VISIBLE);
delete.setVisibility(View.VISIBLE);
}
});


You're trying to look the Views up in the Activity layout, but they are in the View you get as the parameter. You can fix it like this:



...
TextView tel= view.findViewById(R.id.txt_tel);
Button delete= view.findViewById(R.id.btn_delete);
...





share|improve this answer




























    up vote
    1
    down vote













    You can toggle the visibility in the listView row itself. I'd first like to state a problem I found in your code:




    TextView tel= findViewById(R.id.txt_tel);




    it should be TextView tel= view.findViewById(R.id.txt_tel); since the TextView tel is not part of your main layout, but instead the listView's layout.
    Below are two explanations for how to handle the visibility.



    Basic answer:
    In the public View getView method, just set an onClickListener or onTouchListener for whatever item you want to be clicked to make the others visible.
    To toggle the visibility of your TextViews, just do textView.setVisibility(View.VISIBLE); and put this in the onClickListener. All of this code should be in your ListView Adapter.



    Elaborated answer:
    Here is the code



    public class MyAdapter extends ArrayAdapter<MyClass> {
    private Context mContext;
    Activity activity;
    private List<MyClass> class = new ArrayList<>();

    public LogAdapter(Activity a, Context context, ArrayList<MyClass> list) {
    super(context, 0, list);
    mContext = context;
    activity = a;
    class= list;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItem = convertView;
    if (listItem == null){
    //If the row is empty, inflate it with your elements found in the 'mylistview' layout file.
    listItem = LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
    }
    //These names and views are made up. You can change them to suit your needs for which items should become visible on a certain items click.
    Button changeVisibility = listItem.findViewById(R.id.change);
    TextView name = listItem.findViewById(R.id.name);
    TextView surname = listItem.findViewById(R.id.surname);

    changeVisibility.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
    name.setVisibility(View.VISIBLE);
    surname.setVisibility(View.VISIBLE);
    }
    }
    }
    }


    The names, such as MyAdapter and the class MyClass are just examples, you need to make those. Once you do, your Adapter class should look like mine above. You can also change the button and TextView names, it just depends upon what layout file you are inflating your listItem with.



    If you are still having problems with your code, check out this link for an explanation on how to make your custom listView from scratch.
    Hope this helps!






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249112%2fget-a-list-item-from-a-listview%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













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

      TextView tel= findViewById(R.id.txt_tel);
      Button delete= findViewById(R.id.btn_delete);

      tel.setVisibility(View.VISIBLE);
      delete.setVisibility(View.VISIBLE);
      }
      });


      You're trying to look the Views up in the Activity layout, but they are in the View you get as the parameter. You can fix it like this:



      ...
      TextView tel= view.findViewById(R.id.txt_tel);
      Button delete= view.findViewById(R.id.btn_delete);
      ...





      share|improve this answer

























        up vote
        1
        down vote













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

        TextView tel= findViewById(R.id.txt_tel);
        Button delete= findViewById(R.id.btn_delete);

        tel.setVisibility(View.VISIBLE);
        delete.setVisibility(View.VISIBLE);
        }
        });


        You're trying to look the Views up in the Activity layout, but they are in the View you get as the parameter. You can fix it like this:



        ...
        TextView tel= view.findViewById(R.id.txt_tel);
        Button delete= view.findViewById(R.id.btn_delete);
        ...





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









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

          TextView tel= findViewById(R.id.txt_tel);
          Button delete= findViewById(R.id.btn_delete);

          tel.setVisibility(View.VISIBLE);
          delete.setVisibility(View.VISIBLE);
          }
          });


          You're trying to look the Views up in the Activity layout, but they are in the View you get as the parameter. You can fix it like this:



          ...
          TextView tel= view.findViewById(R.id.txt_tel);
          Button delete= view.findViewById(R.id.btn_delete);
          ...





          share|improve this answer












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

          TextView tel= findViewById(R.id.txt_tel);
          Button delete= findViewById(R.id.btn_delete);

          tel.setVisibility(View.VISIBLE);
          delete.setVisibility(View.VISIBLE);
          }
          });


          You're trying to look the Views up in the Activity layout, but they are in the View you get as the parameter. You can fix it like this:



          ...
          TextView tel= view.findViewById(R.id.txt_tel);
          Button delete= view.findViewById(R.id.btn_delete);
          ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 13:56









          Markaos

          508212




          508212
























              up vote
              1
              down vote













              You can toggle the visibility in the listView row itself. I'd first like to state a problem I found in your code:




              TextView tel= findViewById(R.id.txt_tel);




              it should be TextView tel= view.findViewById(R.id.txt_tel); since the TextView tel is not part of your main layout, but instead the listView's layout.
              Below are two explanations for how to handle the visibility.



              Basic answer:
              In the public View getView method, just set an onClickListener or onTouchListener for whatever item you want to be clicked to make the others visible.
              To toggle the visibility of your TextViews, just do textView.setVisibility(View.VISIBLE); and put this in the onClickListener. All of this code should be in your ListView Adapter.



              Elaborated answer:
              Here is the code



              public class MyAdapter extends ArrayAdapter<MyClass> {
              private Context mContext;
              Activity activity;
              private List<MyClass> class = new ArrayList<>();

              public LogAdapter(Activity a, Context context, ArrayList<MyClass> list) {
              super(context, 0, list);
              mContext = context;
              activity = a;
              class= list;
              }
              @NonNull
              @Override
              public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
              View listItem = convertView;
              if (listItem == null){
              //If the row is empty, inflate it with your elements found in the 'mylistview' layout file.
              listItem = LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
              }
              //These names and views are made up. You can change them to suit your needs for which items should become visible on a certain items click.
              Button changeVisibility = listItem.findViewById(R.id.change);
              TextView name = listItem.findViewById(R.id.name);
              TextView surname = listItem.findViewById(R.id.surname);

              changeVisibility.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(final View view) {
              name.setVisibility(View.VISIBLE);
              surname.setVisibility(View.VISIBLE);
              }
              }
              }
              }


              The names, such as MyAdapter and the class MyClass are just examples, you need to make those. Once you do, your Adapter class should look like mine above. You can also change the button and TextView names, it just depends upon what layout file you are inflating your listItem with.



              If you are still having problems with your code, check out this link for an explanation on how to make your custom listView from scratch.
              Hope this helps!






              share|improve this answer



























                up vote
                1
                down vote













                You can toggle the visibility in the listView row itself. I'd first like to state a problem I found in your code:




                TextView tel= findViewById(R.id.txt_tel);




                it should be TextView tel= view.findViewById(R.id.txt_tel); since the TextView tel is not part of your main layout, but instead the listView's layout.
                Below are two explanations for how to handle the visibility.



                Basic answer:
                In the public View getView method, just set an onClickListener or onTouchListener for whatever item you want to be clicked to make the others visible.
                To toggle the visibility of your TextViews, just do textView.setVisibility(View.VISIBLE); and put this in the onClickListener. All of this code should be in your ListView Adapter.



                Elaborated answer:
                Here is the code



                public class MyAdapter extends ArrayAdapter<MyClass> {
                private Context mContext;
                Activity activity;
                private List<MyClass> class = new ArrayList<>();

                public LogAdapter(Activity a, Context context, ArrayList<MyClass> list) {
                super(context, 0, list);
                mContext = context;
                activity = a;
                class= list;
                }
                @NonNull
                @Override
                public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View listItem = convertView;
                if (listItem == null){
                //If the row is empty, inflate it with your elements found in the 'mylistview' layout file.
                listItem = LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
                }
                //These names and views are made up. You can change them to suit your needs for which items should become visible on a certain items click.
                Button changeVisibility = listItem.findViewById(R.id.change);
                TextView name = listItem.findViewById(R.id.name);
                TextView surname = listItem.findViewById(R.id.surname);

                changeVisibility.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                name.setVisibility(View.VISIBLE);
                surname.setVisibility(View.VISIBLE);
                }
                }
                }
                }


                The names, such as MyAdapter and the class MyClass are just examples, you need to make those. Once you do, your Adapter class should look like mine above. You can also change the button and TextView names, it just depends upon what layout file you are inflating your listItem with.



                If you are still having problems with your code, check out this link for an explanation on how to make your custom listView from scratch.
                Hope this helps!






                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can toggle the visibility in the listView row itself. I'd first like to state a problem I found in your code:




                  TextView tel= findViewById(R.id.txt_tel);




                  it should be TextView tel= view.findViewById(R.id.txt_tel); since the TextView tel is not part of your main layout, but instead the listView's layout.
                  Below are two explanations for how to handle the visibility.



                  Basic answer:
                  In the public View getView method, just set an onClickListener or onTouchListener for whatever item you want to be clicked to make the others visible.
                  To toggle the visibility of your TextViews, just do textView.setVisibility(View.VISIBLE); and put this in the onClickListener. All of this code should be in your ListView Adapter.



                  Elaborated answer:
                  Here is the code



                  public class MyAdapter extends ArrayAdapter<MyClass> {
                  private Context mContext;
                  Activity activity;
                  private List<MyClass> class = new ArrayList<>();

                  public LogAdapter(Activity a, Context context, ArrayList<MyClass> list) {
                  super(context, 0, list);
                  mContext = context;
                  activity = a;
                  class= list;
                  }
                  @NonNull
                  @Override
                  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                  View listItem = convertView;
                  if (listItem == null){
                  //If the row is empty, inflate it with your elements found in the 'mylistview' layout file.
                  listItem = LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
                  }
                  //These names and views are made up. You can change them to suit your needs for which items should become visible on a certain items click.
                  Button changeVisibility = listItem.findViewById(R.id.change);
                  TextView name = listItem.findViewById(R.id.name);
                  TextView surname = listItem.findViewById(R.id.surname);

                  changeVisibility.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(final View view) {
                  name.setVisibility(View.VISIBLE);
                  surname.setVisibility(View.VISIBLE);
                  }
                  }
                  }
                  }


                  The names, such as MyAdapter and the class MyClass are just examples, you need to make those. Once you do, your Adapter class should look like mine above. You can also change the button and TextView names, it just depends upon what layout file you are inflating your listItem with.



                  If you are still having problems with your code, check out this link for an explanation on how to make your custom listView from scratch.
                  Hope this helps!






                  share|improve this answer














                  You can toggle the visibility in the listView row itself. I'd first like to state a problem I found in your code:




                  TextView tel= findViewById(R.id.txt_tel);




                  it should be TextView tel= view.findViewById(R.id.txt_tel); since the TextView tel is not part of your main layout, but instead the listView's layout.
                  Below are two explanations for how to handle the visibility.



                  Basic answer:
                  In the public View getView method, just set an onClickListener or onTouchListener for whatever item you want to be clicked to make the others visible.
                  To toggle the visibility of your TextViews, just do textView.setVisibility(View.VISIBLE); and put this in the onClickListener. All of this code should be in your ListView Adapter.



                  Elaborated answer:
                  Here is the code



                  public class MyAdapter extends ArrayAdapter<MyClass> {
                  private Context mContext;
                  Activity activity;
                  private List<MyClass> class = new ArrayList<>();

                  public LogAdapter(Activity a, Context context, ArrayList<MyClass> list) {
                  super(context, 0, list);
                  mContext = context;
                  activity = a;
                  class= list;
                  }
                  @NonNull
                  @Override
                  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                  View listItem = convertView;
                  if (listItem == null){
                  //If the row is empty, inflate it with your elements found in the 'mylistview' layout file.
                  listItem = LayoutInflater.from(mContext).inflate(R.layout.mylistview, parent, false);
                  }
                  //These names and views are made up. You can change them to suit your needs for which items should become visible on a certain items click.
                  Button changeVisibility = listItem.findViewById(R.id.change);
                  TextView name = listItem.findViewById(R.id.name);
                  TextView surname = listItem.findViewById(R.id.surname);

                  changeVisibility.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(final View view) {
                  name.setVisibility(View.VISIBLE);
                  surname.setVisibility(View.VISIBLE);
                  }
                  }
                  }
                  }


                  The names, such as MyAdapter and the class MyClass are just examples, you need to make those. Once you do, your Adapter class should look like mine above. You can also change the button and TextView names, it just depends upon what layout file you are inflating your listItem with.



                  If you are still having problems with your code, check out this link for an explanation on how to make your custom listView from scratch.
                  Hope this helps!







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 11 at 14:05

























                  answered Nov 11 at 13:57









                  Ishaan

                  6991417




                  6991417






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249112%2fget-a-list-item-from-a-listview%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Full-time equivalent

                      さくらももこ

                      13 indicted, 8 arrested in Calif. drug cartel investigation