Android: create CheckBox in activity.java - android

Hi i want to create dynamical number of checkboxes (it depends on the length of array "genre") in an Android app then i did:
CheckBox [] checkbox;
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
checkbox=new CheckBox[genre.length];
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
for(int i=0;i<genre.length;i++){
CheckBox c=new CheckBox(this);
c.setText(genre[i]);
checkbox[i]=c;
}
ArrayAdapter<CheckBox>adapter= new ArrayAdapter<CheckBox>(this, android.R.layout.simple_list_item_1,checkbox);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setAdapter(adapter);
}
but in the listview it shows numbers of the checkboxes in memory(I guess), why?
like : Android.wiget.CheckBox#44f03160
Android.wiget.CheckBox#44f03d60
...

You'd rather use the android.R.simple_list_item_checked item layout and enable multiple-choice on the list:
String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This way you don't have to create the checkboxes manually by yourself

Because of this line
c.setText(genre[i]);

String [] genre={"party","sport","music","café","education"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
ArrayAdapter<String> adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, genre);
ListView lv= (ListView)findViewById(R.id.genre_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(adapter);
}
This looks like checkboxes.

Related

Android Application gets crashed

The app crash because of the following code lines.
I have two string-array in a XML file named as Telephones in a folder named values and two TextView in text.xml.
I can not figure out what's going wrong here.
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1= new ArrayAdapter<String>(this, R.layout.text, number);
listView.setAdapter(arrayAdapter1);
}
}
Your help would be appreciated.
Thank You!
I forgot to add TextView id in MainActivity java but after adding the names_txt won't show up the only one show up is numbers_txt, how fix it please?
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
String[] name= getResources().getStringArray(R.array.names);
String[] number= getResources().getStringArray(R.array.numbers);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.text, R.id.names_txt, name);
listView.setAdapter(arrayAdapter);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, R.layout.text, R.id.numbers_txt, number);
listView.setAdapter(arrayAdapter1);
}
}

add item in to Listview Android?

I am trying to add an item to ListView:
correct?
public class Main extends Activity {
ArrayAdapter <String> listAdapter ;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView)findViewById(R.id.listView);
String[] S= new String[]{"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA-Ali"};
ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll( Arrays.asList(S) );
listAdapter = new ArrayAdapter<String>(this,R.layout.main,planetList);
list.setAdapter(listAdapter);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // Main layout
String[] s = {"YA ali","YA-Ali","YA ali","YA-Ali","YA ali","YA- Ali","YA ali","YA-Ali"};
list = (ListView)findViewById(R.id.listView); // Your activity should have listview with id listView
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s); //android.R.layout.simple_list_item_1 is built-in xml file
list.setAdapter(listAdapter);
}

Simple List View in Android crashes if ArrayList is not empty

I have a Problem with a ListView.
Here is my code:
private void setViewProtocol() {
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.protocol, logs);
ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
}
It crashes - but when I remove the line logs.add("TEST 1") it works.
Can anyone help me? Why does it work when I call it with an empty list?
Thanks
Use the following:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, logs);
If you give a custom layout you have to have an TextView with the ID android.R.id.text1 or you have to override the getView()
Try this way,hope this will help you to solve your problem.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.protocol);
List<String> logs = new ArrayList<String>();
logs.add("TEST 1");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.simple_list_item_1, logs);
ListView lv = (ListView) findViewById(android.R.id.listView1);
lv.setAdapter(adapter);
}

simple_list_item_1 can not be resolved or not a field

I am beginner in android ,I make a simple array list. and bind it to listview my code is
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText textbox1=(EditText)findViewById(R.id.editText1);
ListView listview1=(ListView)findViewById(R.id.listView1);
String[] items={"a1","a2","a3","a4","a5","a6","a7","a8","a9","a10"};
final ArrayList<String> todo=new ArrayList<String>();
final ArrayAdapter<String> aa;
aa=new ArrayAdapter<String>(this, R.layout.simple_list_item_1,items);
listview1.setAdapter(aa);
}
I am getting error in simple_list_item_1 I search on google but all the exmples tell only way to bind not why this error comes.
Change
aa=new ArrayAdapter<String>(this, R.layout.simple_list_item_1,items);
to
aa=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
It's currently trying to take the value of layout.simple_list_item_1 from your app's R class.

How to add an Array of TextViews into a ListView?

I got this piece of code that creates new TextView, then adds it to ArrayList<View> and when it is finished adding TextViews to Array it sets adds that Array into ListView. But somehow my ListView is appearing empty. Any idea what am I doing wrong?
Here is the code:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayList<View> textvs = new ArrayList<View>();
for (int i=0; i<10;i++) {
TextView tv = new TextView(MainActivity.this);
tv.setText(""+i);
textvs.add(tv);
}
lv.addTouchables(portit); // lv is my listview
You should use ArrayAdapter. You did this the wrong way. Here is a sample:
public class ArrayAdapterDemo extends ListActivity {
String[] items = { "this", "is", "a", "really", "silly", "list" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,
items));
}

Categories

Resources