Adding checkboxes into a ListView
up vote
1
down vote
favorite
Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
java
|
show 1 more comment
up vote
1
down vote
favorite
Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
java
how exactly it doesn't work?
– Vladyslav Matviienko
Nov 9 at 13:05
The app just crashes when testing it. It's the listView.addView that's causing the crash I believe.
– NZX
Nov 9 at 13:11
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Nov 9 at 13:11
No I don't think so. It's something with the code.
– NZX
Nov 9 at 13:16
Where is adapter? It can not work without adapter.
– Milos Lulic
Nov 9 at 13:19
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
java
Hello I'm currently trying to make my first app on android and I've gotten some problems. I'm trying to make a todo-list app so want some kind of input to be transformed into checkboxes. I've made it work with radio buttons using radioGroup. But when using Checkboxes with ListView it just doesn't work.
Here's my code:
public class MainActivity extends AppCompatActivity {
EditText t;
ListView listView;
ArrayList<CheckBox> checkList = new ArrayList<>();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
t = findViewById(R.id.input);
String s = t.getText().toString();
CheckBox check = new CheckBox(this);
checkList.add(check);
listView = findViewById(R.id.list);
checkList.get(i).setText(s);
listView.addView(checkList.get(i));
i++;
t.setText("");
}}
The app crashes saying something about adapterView
java
java
edited Nov 9 at 13:15
asked Nov 9 at 13:03
NZX
92
92
how exactly it doesn't work?
– Vladyslav Matviienko
Nov 9 at 13:05
The app just crashes when testing it. It's the listView.addView that's causing the crash I believe.
– NZX
Nov 9 at 13:11
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Nov 9 at 13:11
No I don't think so. It's something with the code.
– NZX
Nov 9 at 13:16
Where is adapter? It can not work without adapter.
– Milos Lulic
Nov 9 at 13:19
|
show 1 more comment
how exactly it doesn't work?
– Vladyslav Matviienko
Nov 9 at 13:05
The app just crashes when testing it. It's the listView.addView that's causing the crash I believe.
– NZX
Nov 9 at 13:11
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Nov 9 at 13:11
No I don't think so. It's something with the code.
– NZX
Nov 9 at 13:16
Where is adapter? It can not work without adapter.
– Milos Lulic
Nov 9 at 13:19
how exactly it doesn't work?
– Vladyslav Matviienko
Nov 9 at 13:05
how exactly it doesn't work?
– Vladyslav Matviienko
Nov 9 at 13:05
The app just crashes when testing it. It's the listView.addView that's causing the crash I believe.
– NZX
Nov 9 at 13:11
The app just crashes when testing it. It's the listView.addView that's causing the crash I believe.
– NZX
Nov 9 at 13:11
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Nov 9 at 13:11
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Nov 9 at 13:11
No I don't think so. It's something with the code.
– NZX
Nov 9 at 13:16
No I don't think so. It's something with the code.
– NZX
Nov 9 at 13:16
Where is adapter? It can not work without adapter.
– Milos Lulic
Nov 9 at 13:19
Where is adapter? It can not work without adapter.
– Milos Lulic
Nov 9 at 13:19
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
- Create a new layout file (say
custom_cell.xml) inside your layouts folder. - Insert a checkbox inside your
custom_cell.xmland place an Id which you can use later to identify the checkbox - Create an adapter for your listview
- Override the
getViewmethod - Inside the
getViewmethod, inflate a new cell using thecustom_cell.xmlor reuse an existing cell if provided - Reference the Checkbox by using the Id you provided in the
custom_cell.xmlfile
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
- Create a new layout file (say
custom_cell.xml) inside your layouts folder. - Insert a checkbox inside your
custom_cell.xmland place an Id which you can use later to identify the checkbox - Create an adapter for your listview
- Override the
getViewmethod - Inside the
getViewmethod, inflate a new cell using thecustom_cell.xmlor reuse an existing cell if provided - Reference the Checkbox by using the Id you provided in the
custom_cell.xmlfile
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
add a comment |
up vote
0
down vote
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
- Create a new layout file (say
custom_cell.xml) inside your layouts folder. - Insert a checkbox inside your
custom_cell.xmland place an Id which you can use later to identify the checkbox - Create an adapter for your listview
- Override the
getViewmethod - Inside the
getViewmethod, inflate a new cell using thecustom_cell.xmlor reuse an existing cell if provided - Reference the Checkbox by using the Id you provided in the
custom_cell.xmlfile
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
add a comment |
up vote
0
down vote
up vote
0
down vote
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
- Create a new layout file (say
custom_cell.xml) inside your layouts folder. - Insert a checkbox inside your
custom_cell.xmland place an Id which you can use later to identify the checkbox - Create an adapter for your listview
- Override the
getViewmethod - Inside the
getViewmethod, inflate a new cell using thecustom_cell.xmlor reuse an existing cell if provided - Reference the Checkbox by using the Id you provided in the
custom_cell.xmlfile
You're unfortunately doing something wrong here. You are trying to add your view to Listviews on
listView.addView(checkList.get(i));
This is not the correct way to use ListViews. You have to use an adapter. The adapter would need to provide data for the listview to load.
Please have a look at this tutorial for how to use listviews.
Below is a summary of the steps to use a listView correctly.
- Create a new layout file (say
custom_cell.xml) inside your layouts folder. - Insert a checkbox inside your
custom_cell.xmland place an Id which you can use later to identify the checkbox - Create an adapter for your listview
- Override the
getViewmethod - Inside the
getViewmethod, inflate a new cell using thecustom_cell.xmlor reuse an existing cell if provided - Reference the Checkbox by using the Id you provided in the
custom_cell.xmlfile
edited Nov 9 at 14:31
answered Nov 9 at 13:21
Ruchira Randana
2,58811820
2,58811820
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
add a comment |
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Is it a must to write all the code inside the protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } or is it ok to put it below as I did?
– NZX
Nov 9 at 13:52
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
Hi @NZX: The problem is not where you have inserted the code. You can not use a listview without using an adapter. Specifically saying, the line of code "listView.addView(checkList.get(i));" is wrong. You can not add a view like this to a listview. I'll define the correct way in my answer.
– Ruchira Randana
Nov 9 at 14:23
add a comment |
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%2f53226239%2fadding-checkboxes-into-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
how exactly it doesn't work?
– Vladyslav Matviienko
Nov 9 at 13:05
The app just crashes when testing it. It's the listView.addView that's causing the crash I believe.
– NZX
Nov 9 at 13:11
Possible duplicate of Unfortunately MyApp has stopped. How can I solve this?
– Vladyslav Matviienko
Nov 9 at 13:11
No I don't think so. It's something with the code.
– NZX
Nov 9 at 13:16
Where is adapter? It can not work without adapter.
– Milos Lulic
Nov 9 at 13:19