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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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 android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.xml and place an Id which you can use later to identify the checkbox

  • Create an adapter for your listview

  • Override the getView method

  • Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided

  • Reference the Checkbox by using the Id you provided in the custom_cell.xml file






share|improve this answer























  • 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











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%2f53226239%2fadding-checkboxes-into-a-listview%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























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.xml and place an Id which you can use later to identify the checkbox

  • Create an adapter for your listview

  • Override the getView method

  • Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided

  • Reference the Checkbox by using the Id you provided in the custom_cell.xml file






share|improve this answer























  • 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















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.xml and place an Id which you can use later to identify the checkbox

  • Create an adapter for your listview

  • Override the getView method

  • Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided

  • Reference the Checkbox by using the Id you provided in the custom_cell.xml file






share|improve this answer























  • 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













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.xml and place an Id which you can use later to identify the checkbox

  • Create an adapter for your listview

  • Override the getView method

  • Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided

  • Reference the Checkbox by using the Id you provided in the custom_cell.xml file






share|improve this answer














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.xml and place an Id which you can use later to identify the checkbox

  • Create an adapter for your listview

  • Override the getView method

  • Inside the getView method, inflate a new cell using the custom_cell.xml or reuse an existing cell if provided

  • Reference the Checkbox by using the Id you provided in the custom_cell.xml file







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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

Landwehr

Reims

Schenkenzell