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.
Should become this
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
add a comment |
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.
Should become this
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
1
Show some code pls.
– W. Seun
Nov 11 at 13:18
1
Read the documentation forListView.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
add a comment |
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.
Should become this
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
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.
Should become this
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
android android-studio
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 forListView.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
add a comment |
1
Show some code pls.
– W. Seun
Nov 11 at 13:18
1
Read the documentation forListView.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
add a comment |
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 View
s 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);
...
add a comment |
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!
add a comment |
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 View
s 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);
...
add a comment |
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 View
s 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);
...
add a comment |
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 View
s 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);
...
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 View
s 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);
...
answered Nov 11 at 13:56
Markaos
508212
508212
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
edited Nov 11 at 14:05
answered Nov 11 at 13:57
Ishaan
6991417
6991417
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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